PCRB webassembly
This commit is contained in:
31
MesaFabApproval.API/Utilities/FileUtilities.cs
Normal file
31
MesaFabApproval.API/Utilities/FileUtilities.cs
Normal file
@ -0,0 +1,31 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user