public async Task <IActionResult> UploadInterpretation([FromRoute] int id, [FromForm] InterpretationDataViewModel interpretationDataViewModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != interpretationDataViewModel.ID) { return(BadRequest()); } var interpretation = _context.Interpretations.Where(i => i.CannedIndicatorId == interpretationDataViewModel.CannedId).Where(i => i.ID == id).First(); interpretation.Message = interpretationDataViewModel.Message; if (interpretationDataViewModel.File.Length > 0) { var splittedName = interpretationDataViewModel.File.FileName.Split("."); var attachment = new Attachment { Filename = interpretationDataViewModel.File.FileName, InterpretationId = interpretation.ID, Mime = interpretationDataViewModel.File.ContentType, Extension = splittedName[1] }; var path = Path.Combine(_hostingEnviroment.WebRootPath, "pdfs", attachment.Newname); using (var stream = new FileStream(path, FileMode.Create)) { interpretationDataViewModel.File.CopyTo(stream); stream.Position = 0; stream.Close(); } _context.Attachments.Add(attachment); } try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!InterpretationExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PostInterpretationWithAttachment([FromForm] InterpretationDataViewModel interpretationDataViewModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var interpretation = new Interpretation { Message = interpretationDataViewModel.Message, CannedIndicatorId = interpretationDataViewModel.CannedId }; _context.Interpretations.Add(interpretation); var splittedName = interpretationDataViewModel.File.FileName.Split("."); var attachment = new Attachment { Filename = interpretationDataViewModel.File.FileName, InterpretationId = interpretation.ID, Mime = interpretationDataViewModel.File.ContentType, Extension = splittedName[1] }; _context.Attachments.Add(attachment); var path = Path.Combine(_hostingEnviroment.WebRootPath, "pdfs", attachment.Newname); using (var stream = new FileStream(path, FileMode.Create)) { interpretationDataViewModel.File.CopyTo(stream); stream.Position = 0; stream.Close(); } await _context.SaveChangesAsync(); return(Ok()); }