public ActionResult Create(Tech rec) { if (ModelState.IsValid) { HttpPostedFileBase file = Request.Files["file"]; if (file != null && file.FileName != "") { string ext = Path.GetExtension(file.FileName); string filename = Guid.NewGuid().ToString() + ext; string filePath = Path.Combine(HttpContext.Server.MapPath("/Uploads"), filename); file.SaveAs(filePath); rec.FileUrl = "/Uploads/" + filename; } db.Tech.Add(rec); db.SaveChanges(); return RedirectToAction("Event", "Admin"); } return View(rec); }
public ActionResult Edit(Tech rec) { if (ModelState.IsValid) { HttpPostedFileBase file = Request.Files["file"]; DbEntities tempdb = new DbEntities(); Tech ori = tempdb.Tech.Find(rec.Id); if (file != null && file.FileName != "") { if (ori.FileUrl != null) { string oriFilePath = Path.Combine(HttpContext.Server.MapPath("/Uploads"), Path.GetFileName(ori.FileUrl)); if (System.IO.File.Exists(oriFilePath)) { System.IO.File.Delete(oriFilePath); } } string ext = Path.GetExtension(file.FileName); string filename = Guid.NewGuid().ToString() + ext; string filePath = Path.Combine(HttpContext.Server.MapPath("/Uploads"), filename); file.SaveAs(filePath); rec.FileUrl = "/Uploads/" + filename; } else { rec.FileUrl = ori.FileUrl; } db.Entry(rec).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Event", "Admin"); } return View(rec); }