public ActionResult UploadFile(FormCollection forms, HttpPostedFileBase file) // { string fileName = ""; int projectid = Convert.ToInt32(forms["projectId"]); int gateid = Convert.ToInt32(forms["gateId"]); int line = Convert.ToInt32(forms["lineNo"]); int attachId = Convert.ToInt32(forms["attachId"]); if (attachId > 0 && file is null) { //System.Diagnostics.Debug.WriteLine("delete attachement"); DeleteAttachment(attachId); } else { try { fileName = Path.GetFileName(file.FileName); string filepath = Path.Combine(Server.MapPath("~/UploadedFiles"), fileName); file.SaveAs(filepath); } catch { return(RedirectToAction("GateDetail", "Project", new { @projectId = projectid, @gateId = gateid, @gateLine = line })); } using (DatabaseContext db = new DatabaseContext()) { EstimateAttachment ea = new EstimateAttachment(); ea.projectId = projectid; ea.gateId = gateid; ea.lineNo = line; ea.fileName = fileName; db.EstimateAttachments.Add(ea); db.SaveChanges(); } // System.Diagnostics.Debug.WriteLine("upload file"); } return(RedirectToAction("GateDetail", "Project", new { @projectId = projectid, @gateId = gateid, @gateLine = line })); }
public string DeleteAttachment(int id) { int projectid = 0; int gateid = 0; int line = 0; string result = ""; using (DatabaseContext db = new DatabaseContext()) { try { EstimateAttachment ea = db.EstimateAttachments.Where(x => x.attacheId == id).First(); // db.EstimateAttachments.Add(ea); projectid = ea.projectId; gateid = ea.gateId; line = ea.lineNo; db.Entry(ea).State = EntityState.Deleted; db.SaveChanges(); string fullPath = Request.MapPath("~/UploadedFiles/" + ea.fileName); if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); } result = "Success"; } catch (Exception ex) { result = "No"; MyLogger.GetInstance().Error(ex.Message); } } return(result); }