public async Task Add(ResourceAttachModel file) { try { if (file.File != null) { string path = "/Files/" + file.FileName; using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create)) { await file.File.CopyToAsync(fileStream); } ResourceAttachment model = new ResourceAttachment { FileName = file.FileName, Path = file.Path, ResourceId = file.ResourceId }; await _uow.Repository <ResourceAttachment>().Add(model); await _uow.SaveChangesAsync(); } } catch (Exception ex) { ex.Data[nameof(ResourceAttachModel)] = file; throw new Exception("Error when adding file {nameof(ProjectType)}", ex); } }
public IActionResult UploadFile([FromBody] ResourceAttachModel resource) { if (resource == null) { throw new ArgumentException($"{nameof(resource)} cannot be null"); } _attachment.Add(resource); return(Ok(resource)); }