/// <inheritdoc/> public async Task <TempFileLog> UploadFileAsync(IFormFile formFile) { try { string saveDirectory = this.hostingEnvironment.GetTempUploadDirectory(); string resultFileName = FilesFunctions.GetUniqueFileName(); string resultFileExtension = formFile.FileName.Split('.').LastOrDefault(); string relativeSaveDirectory = Path.Combine(Folders.PrivateRootFolderName, Folders.UploadFolderName, Folders.TempFolderName); string fileFullPath = Path.Combine(saveDirectory, $"{resultFileName}.{resultFileExtension}"); string fileRelativePath = Path.Combine(relativeSaveDirectory, $"{resultFileName}.{resultFileExtension}"); using (FileStream stream = System.IO.File.Create(fileFullPath)) { formFile.CopyTo(stream); stream.Flush(); } TempFileLog fileEntity = new TempFileLog { Name = resultFileName, Path = fileRelativePath, FileExtension = FilesFunctions.GetFileExtension(resultFileExtension), }; fileEntity.FileType = FilesFunctions.GetFileType(fileEntity.FileExtension); this.loggerContext.TempFileLogs.Add(fileEntity); await this.loggerContext.SaveChangesAsync(); return(fileEntity); } catch (Exception ex) { await this.logger.LogErrorAsync(ex); return(default);
public async Task <FileDto> UploadFileAsync(IFormFile formFile) { try { string saveDirectory = this.hostingEnvironment.GetPrivateRootTempUploadDirectory(); string resultFileName = FilesFunctions.GetUniqueFileName(); string resultFileExtension = formFile.FileName.Split('.').LastOrDefault(); string relativeSaveDirectory = Path.Combine(Constants.PrivateRootFolderName, Constants.UploadFolderName, Constants.TempFolderName); string fileFullPath = Path.Combine(saveDirectory, $"{resultFileName}.{resultFileExtension}"); string fileRelativePath = Path.Combine(relativeSaveDirectory, $"{resultFileName}.{resultFileExtension}"); using (FileStream stream = System.IO.File.Create(fileFullPath)) { formFile.CopyTo(stream); stream.Flush(); } Mvc567.Entities.Database.File fileEntity = new Mvc567.Entities.Database.File { Name = resultFileName, Path = fileRelativePath, Temp = true, FileExtension = FilesFunctions.GetFileExtension(resultFileExtension) }; fileEntity.FileType = FilesFunctions.GetFileType(fileEntity.FileExtension); this.uow.GetStandardRepository().Add <Mvc567.Entities.Database.File>(fileEntity); await this.uow.SaveChangesAsync(); var fileDto = this.mapper.Map <FileDto>(fileEntity); return(fileDto); } catch (Exception ex) { await this.LogErrorAsync(ex, nameof(UploadFileAsync)); return(null); } }