public ActionResult InsertPortfolioCategory() { if (Session["UserID"] == null && Session["UserName"] == null) { return(RedirectToAction("Login", "Login")); } try { tblPortfolioGalleryCategory PGC = new tblPortfolioGalleryCategory(); PGC.PortfolioGalleryCategoryName = Request.Form["CategoryName"]; if (ModelState.IsValid) { int fileSize = 0; string fileName = string.Empty; string mimeType = string.Empty; System.IO.Stream fileContent; if (Request.Files.Count > 0) { HttpPostedFileBase file = Request.Files[0]; 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 = "Profile pic format must be JPEG or JPG or PNG." }, JsonRequestBehavior.AllowGet)); } #region Save And compress file //To save file, use SaveAs method file.SaveAs(Server.MapPath("~/PortfolioCategory/") + fileName); if (!ImageProcessing.InsertImages(Server.MapPath("~/PortfolioCategory/") + fileName)) { return(Json(new { success = false, message = "Error occur while uploading image." }, JsonRequestBehavior.AllowGet)); } #endregion } PGC.PortfolioGalleryCategoryImage = fileName; PGC.CreatedDate = DateTime.Now; db.tblPortfolioGalleryCategories.Add(PGC); db.SaveChanges(); } } catch (Exception ex) { return(Json(new { success = false, message = "Record Not inserted" }, JsonRequestBehavior.AllowGet)); } return(Json(new { success = true, message = "Record inserted" }, JsonRequestBehavior.AllowGet)); }
public ActionResult DeletePortfolioCategory(int id) { if (Session["UserID"] == null && Session["UserName"] == null) { return(RedirectToAction("Login", "Login")); } try { tblPortfolioGalleryCategory PGC = db.tblPortfolioGalleryCategories.Find(id); string path = Server.MapPath("~/PortfolioCategory/" + PGC.PortfolioGalleryCategoryImage); FileInfo delfile = new FileInfo(path); delfile.Delete(); db.tblPortfolioGalleryCategories.Remove(PGC); db.SaveChanges(); return(Json(new { success = true, message = "Record deleted" }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(new { success = false, message = "Error occur!" + ex.Message }, JsonRequestBehavior.AllowGet)); } }