private string UploadedFile(ReportCaseViewModel model) { string uniqueFileName = null; if (model.UploadPicture != null) { string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(model.UploadPicture.FileName); string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.UploadPicture.CopyTo(fileStream); } } return(uniqueFileName); }
public async Task <IActionResult> ReportCase(ReportCaseViewModel model) { if (ModelState.IsValid) { string uniqueFileName = UploadedFile(model); Case myCase = new Case { Description = model.Description, AccidentDate = model.AccidentDate, CaseNumber = model.CaseNumber, //User = model.User, Picture = uniqueFileName, UserId = model.UserId }; _context.Add(myCase); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View()); }