/// <summary> /// Save file /// </summary> /// <param name="file"></param> /// <returns></returns> public async Task <int> Save(IFormFile file) { try { var fileInfo = new File() { ContentType = file.ContentType, Extension = file.FileName.Split(".").LastOrDefault(), OriginalName = file.FileName, Path = _fileSettings.DefaultFilesUploadLocation, CreateDate = DateTime.Now }; fileInfo.Path += fileInfo.Key; //Upload to disk file.UploadToDisk(fileInfo.Path); //Save file information to db _context.Files.Add(fileInfo); await _context.SaveChangesAsync(); return(fileInfo.Id); } catch { throw; } }
public async Task <IActionResult> Post([FromBody] UploadFileDto newFile) { var model = new File { Content = newFile.Content, MimeType = newFile.MimeType, DisplayName = newFile.DisplayName, UploadDate = DateTimeOffset.UtcNow, ModifyDate = DateTimeOffset.UtcNow }; _dbContext.Files.Add(model); await _dbContext.SaveChangesAsync(); return(CreatedAtAction(nameof(Get), new { fileGuid = model.Id }, model)); }
public async Task <bool> AddFile(File file) { try { // ToDo catch exception and log filesDbContext.Files.Add(file); await filesDbContext.SaveChangesAsync(); return(true); } catch { // Todo Log actual Exception/stack trace logger.LogError($"Failed to Save {nameof(File)} to DB"); return(false); } }
public async Task SaveChanges() { await _context.SaveChangesAsync(); }