public ActionResult Create([Bind(Include = "CycleCountFId,CycleCountDateTime,CustomerPn,SageQty,PortalQty,ActualQty,SageAdjQty,PortalAdjQty,LocationsCounted,CountedBy,AuditedBy,CountOff,CorrectedBy,CorrectedDateTime,Notes")] CycleCountF cycleCountF) { if (ModelState.IsValid) { List <FileCycleCount> fileCycleCounts = new List <FileCycleCount>(); for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); FileCycleCount fileCycleCount = new FileCycleCount() { FileName = fileName, Extension = Path.GetExtension(fileName), Id = Guid.NewGuid() }; fileCycleCounts.Add(fileCycleCount); var path = Path.Combine(Server.MapPath("~/images/"), fileCycleCount.Id + fileCycleCount.Extension); file.SaveAs(path); } } cycleCountF.FileCycleCounts = fileCycleCounts; db.CycleCountFs.Add(cycleCountF); db.SaveChanges(); return(RedirectToAction("Index")); } return(View()); //return View(cycleCountF); }
public JsonResult DeleteFile(string id) { if (String.IsNullOrEmpty(id)) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(new { Result = "Error" })); } try { Guid guid = new Guid(id); FileCycleCount fileCycleCount = db.FileCycleCounts.Find(guid); if (fileCycleCount == null) { Response.StatusCode = (int)HttpStatusCode.NotFound; return(Json(new { Result = "Error" })); } //Remove from database db.FileCycleCounts.Remove(fileCycleCount); db.SaveChanges(); //Delete file from the file system var path = Path.Combine(Server.MapPath("~/images/"), fileCycleCount.Id + fileCycleCount.Extension); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } return(Json(new { Result = "OK" })); } catch (Exception ex) { return(Json(new { Result = "ERROR", Message = ex.Message })); } }