When debugging only app.Services.GetRequiredService<IPCRBService>(); Injected AppSettings instead of using GetEnvironmentVariable at Services level Get ready to use VSCode IDE
31 lines
1.1 KiB
C#
31 lines
1.1 KiB
C#
using Microsoft.AspNetCore.Components.Forms;
|
|
|
|
using NLog;
|
|
|
|
namespace MesaFabApproval.API.Utilities;
|
|
|
|
public class FileUtilities {
|
|
private static readonly Logger _logger = NLog.LogManager.GetCurrentClassLogger();
|
|
|
|
public static async Task SaveFileToFileSystem(IFormFile file, string path) {
|
|
try {
|
|
_logger.Info($"Attempting to save file to file system");
|
|
|
|
if (file is null) throw new ArgumentNullException("File cannot be null");
|
|
if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("Path cannot be null or empty");
|
|
|
|
if (File.Exists(path)) throw new Exception($"A file already exists with name {file.FileName}");
|
|
|
|
string? directoryPath = Path.GetDirectoryName(path);
|
|
if (!string.IsNullOrWhiteSpace(directoryPath))
|
|
Directory.CreateDirectory(directoryPath);
|
|
|
|
using (Stream stream = File.Create(path)) {
|
|
await file.CopyToAsync(stream);
|
|
}
|
|
} catch (Exception ex) {
|
|
_logger.Error($"Unable to save file to file system, because {ex.Message}");
|
|
throw;
|
|
}
|
|
}
|
|
} |