public async Task <IActionResult> Edit(int id, [Bind("ItemId,Title,DiaryDate,Description,DiaryImage")] DiaryReport diaryReport, IFormFile file) { if (id != diaryReport.ItemId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(diaryReport); await _context.SaveChangesAsync(); if (file != null || file.Length != 0) { // Create a File Info FileInfo fi = new FileInfo(file.FileName); // This code creates a unique file name to prevent duplications // stored at the file location var newFilename = diaryReport.ItemId + "_" + String.Format("{0:d}", (DateTime.Now.Ticks / 10) % 100000000) + fi.Extension; var webPath = hostingEnvironment.WebRootPath; var path = Path.Combine("", webPath + @"\ImageFiles\" + newFilename); // IMPORTANT: The pathToSave variable will be save on the column in the database var pathToSave = @"/ImageFiles/" + newFilename; // This stream the physical file to the allocate wwwroot/ImageFiles folder using (var stream = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(stream); } // This save the path to the record diaryReport.DiaryImage = pathToSave; _context.Update(diaryReport); await _context.SaveChangesAsync(); } } catch (DbUpdateConcurrencyException) { if (!DiaryReportExists(diaryReport.ItemId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(diaryReport)); }
protected void Page_Unload(object sender, EventArgs e) { ReportDocument RptDoc = new ReportDocument(); if (RptDoc != null) { RptDoc.Close(); RptDoc.Dispose(); RptDoc = null; DiaryReport.Dispose(); DiaryReport = null; } }