public void DeleteImageByID(int id) { tblPortfolioGallery tblPortfolioGallery = db.tblPortfolioGalleries.Find(id); db.tblPortfolioGalleries.Remove(tblPortfolioGallery); string path = Server.MapPath("~/PortfolioGalleryImages/" + tblPortfolioGallery.PortfolioGalleryImage); FileInfo delfile = new FileInfo(path); delfile.Delete(); db.SaveChanges(); }
public ActionResult DeleteImage(int id) { if (Session["UserID"] == null && Session["UserName"] == null) { return(RedirectToAction("Login", "Login")); } try { tblPortfolioGallery tblPortfolioGallery = db.tblPortfolioGalleries.Find(id); db.tblPortfolioGalleries.Remove(tblPortfolioGallery); string path = Server.MapPath("~/PortfolioGalleryImages/" + tblPortfolioGallery.PortfolioGalleryImage); FileInfo delfile = new FileInfo(path); delfile.Delete(); db.SaveChanges(); return(Json(new { success = true, message = "Image is deleted." }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(new { success = false, message = "Image is not deleted." }, JsonRequestBehavior.AllowGet)); } }
public ActionResult InsertImage() { if (Session["UserID"] == null && Session["UserName"] == null) { return(RedirectToAction("Login", "Login")); } try { if (ModelState.IsValid) { int PortfolioID = Convert.ToInt32(Request.Form["PortfolioID"]); int CategoryID = Convert.ToInt32(Request.Form["CategoryID"]); if (IsGalleryCapacityOverflow(CategoryID, PortfolioID, Request.Files.Count)) { return(Json(new { success = false, message = "Selected category is going to be overflow! Insert 25 images only for single category" }, JsonRequestBehavior.AllowGet)); } if (Request.Files.Count > 25) { return(Json(new { success = false, message = "You can not insert more than 25 Images in single category" }, JsonRequestBehavior.AllowGet)); } if (Request.Files.Count > 0) { int fileSize = 0; string fileName = string.Empty; string mimeType = string.Empty; System.IO.Stream fileContent; for (int i = 0; i < Request.Files.Count; i++) { tblPortfolioGallery portfolioGallery = new tblPortfolioGallery(); portfolioGallery.PortfolioID = PortfolioID; portfolioGallery.PortfolioGalleryCategoryID = CategoryID; HttpPostedFileBase file = Request.Files[i]; fileSize = file.ContentLength; fileName = file.FileName; mimeType = file.ContentType; fileContent = file.InputStream; if (mimeType.ToLower() != "image/jpeg" && mimeType.ToLower() != "image/jpg" && mimeType.ToLower() != "image/png") { return(Json(new { Formatwarning = true, message = "Image format must be JPEG or JPG or PNG." }, JsonRequestBehavior.AllowGet)); } //WebImage img = new WebImage(file.InputStream); #region Save And compress file //To save file, use SaveAs method file.SaveAs(Server.MapPath("~/PortfolioGalleryImages/") + fileName); if (!ImageProcessing.InsertPortfolioImages(Server.MapPath("~/PortfolioGalleryImages/") + fileName)) { return(Json(new { success = false, message = "Error occur while uploading image." }, JsonRequestBehavior.AllowGet)); } #endregion portfolioGallery.PortfolioGalleryImage = fileName; portfolioGallery.CreatedDate = DateTime.Now; db.tblPortfolioGalleries.Add(portfolioGallery); db.SaveChanges(); } } return(Json(new { success = true, message = "Record inserted successfully" }, JsonRequestBehavior.AllowGet)); } else { return(Json(new { success = false, message = "Record not inserted" }, JsonRequestBehavior.AllowGet)); } } catch (Exception ex) { return(Json(new { success = false, message = "Error!" + ex.Message }, JsonRequestBehavior.AllowGet)); } }