public ActionResult DeleteConfirmed(int id) { TechnicalFieldReport technicalFieldReport = db.TechnicalFieldReports.Find(id); // get the filename, if one exists if (technicalFieldReport.Filename != null) { var fileName = Path.GetFileName(technicalFieldReport.Filename); // get the path to the file var path = Path.Combine(Server.MapPath("~/Files"), fileName); // delete the file try { if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } } catch (Exception) { ModelState.AddModelError("uploadError", "Can't delete the file."); } } db.TechnicalFieldReports.Remove(technicalFieldReport); db.SaveChanges(); return(RedirectToAction("Index")); }
// GET: /TechnicalFieldReports/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } TechnicalFieldReport technicalFieldReport = db.TechnicalFieldReports.Find(id); if (technicalFieldReport == null) { return(HttpNotFound()); } return(View(technicalFieldReport)); }
public ActionResult Create([Bind(Include = "ID,Title,Filename,DisplayOrder")] TechnicalFieldReport technicalFieldReport, HttpPostedFileBase file) { if (file != null) { // if file's content length is zero or no files submitted if (Request.Files.Count != 1 || Request.Files[0].ContentLength == 0) { ModelState.AddModelError("uploadError", "Please select a file to upload."); return(View(technicalFieldReport)); } // check the file size (max 4 Mb) //if (Request.Files[0].ContentLength > 1024 * 1024 * 4) // check the file size (max 20 Mb) if (Request.Files[0].ContentLength > 5120 * 5120 * 20) { ModelState.AddModelError("uploadError", "File size can't exceed 20 MB"); return(View(technicalFieldReport)); } // check file extension string extension = Path.GetExtension(Request.Files[0].FileName).ToLower(); if (extension != ".pdf") { ModelState.AddModelError("uploadError", "Please select a PDF to upload."); return(View(technicalFieldReport)); } // extract only the filename var fileName = Path.GetFileName(file.FileName); // store the file inside ~/Images folder var path = Path.Combine(Server.MapPath("~/Files"), fileName); try { if (!System.IO.File.Exists(path)) { Request.Files[0].SaveAs(path); //System.IO.File.Delete(path); } else { ModelState.AddModelError("uploadError", "An file with that name already exists. Please rename your file."); return(View(technicalFieldReport)); } } catch (Exception) { ModelState.AddModelError("uploadError", "Can't save file to disk"); } } if (ModelState.IsValid) { if (file != null) { technicalFieldReport.Filename = file.FileName; } else { technicalFieldReport.Filename = null; } db.TechnicalFieldReports.Add(technicalFieldReport); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(technicalFieldReport)); }