public ActionResult DeleteAttachment(int id) { RequirementAttachment ra = db.RequirementAttachments.Find(id); db.RequirementAttachments.Remove(ra); if (db.SaveChanges() > 0) { string fullPath = Request.MapPath(ra.Location); if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); } } return(RedirectToAction("Details", new { @id = ra.RequirementId })); }
public ActionResult UploadAttachment(HttpPostedFileBase Attachment) { int requirementId = Convert.ToInt16(Request["RequirementId"]); Requirement r = db.Requirements.FirstOrDefault(e => e.RequirementId == requirementId); if (Attachment != null) { string path = Server.MapPath("~/Content/Files/"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string ext = Path.GetExtension(Attachment.FileName); try { Attachment.SaveAs(path + Attachment.FileName); } catch (Exception ex) { TempData["upload"] = 0; TempData["errorMessage"] = ex.Message; return(RedirectToAction("Details", new { @id = requirementId })); } string savePath = "/Content/Files/" + Attachment.FileName; RequirementAttachment ra = new RequirementAttachment(); ra.RequirementId = requirementId; ra.Name = Attachment.FileName; ra.Location = savePath; ra.DateCreated = DateTime.UtcNow.AddHours(8); db.RequirementAttachments.Add(ra); db.SaveChanges(); return(RedirectToAction("Details", new { @id = requirementId })); } return(RedirectToAction("Details", new { @id = requirementId })); }