public ActionResult SaveChanges(PostAd Ads) { Ads.CategoryId = Convert.ToInt32(Session["CategoryId"]); Ads.ProductId = Convert.ToInt32(Session["ProductId"]); Ads.UserId = Convert.ToInt32(Session["UserId"]); //HttpPostedFileBase file1 = Request.Files["file1"]; //HttpPostedFileBase file2 = Request.Files["file2"]; //HttpPostedFileBase file3 = Request.Files["file3"]; //#region VariableDeclaration //string filename1 = Path.GetFileName(file1.FileName); //string filename2 = Path.GetFileName(file2.FileName); //string filename3 = Path.GetFileName(file3.FileName); //string DefaultImage = "Default.jpg"; //string fileExtension1 = Path.GetExtension(file1.FileName); //if (fileExtension1 == "") // fileExtension1 = ".jpg"; //string fileExtension2 = Path.GetExtension(file2.FileName); //if (fileExtension2 == "") // fileExtension2 = ".jpg"; //string fileExtension3 = Path.GetExtension(file3.FileName); //if (fileExtension3 == "") // fileExtension3 = ".jpg"; //int fileSize1 = file1.ContentLength; //if (fileSize1 == 0) // fileSize1 = 500000; //int fileSize2 = file2.ContentLength; //if (fileSize2 == 0) // fileSize2 = 500000; //int fileSize3 = file3.ContentLength; //if (fileSize3 == 0) // fileSize3 = 500000; //#endregion //if (filename1 == "" && filename2 == "" && filename3 == "") //{ // Session["AllImageNull"] = "Please upload atleast one photo of your Product"; //} //else //{ // //if ((fileExtension1.ToLower() != ".jpg" || fileExtension1.ToLower() != ".jpeg" || fileExtension1.ToLower() != ".png") && (fileExtension2.ToLower() != ".jpg" || fileExtension2.ToLower() != ".jpeg" || fileExtension2.ToLower() != ".png") && (fileExtension3.ToLower() != ".jpg" || fileExtension3.ToLower() != ".jpeg" || fileExtension3.ToLower() != ".png")) // //{ // // Session["FileExtensionError"] = "File extension does't match."; // //} // //else // //{ // if (fileSize1 > 512000 || fileSize2 > 512000 || fileSize3 > 512000) // { // Session["FileSizeError"] = "Product image size exceed the maximum size of 500KB."; // } // else // { // if (filename1 != "" && filename2 != "" && filename3 != "") // { // file1.SaveAs(Server.MapPath("~/Content/Images/img1/" + filename1)); // file2.SaveAs(Server.MapPath("~/Content/Images/img2/" + filename2)); // file3.SaveAs(Server.MapPath("~/Content/Images/img3/" + filename3)); // string filepath1 = "../../Content/Images/img1/" + filename1; // string filepath2 = "../../Content/Images/img2/" + filename2; // string filepath3 = "../../Content/Images/img3/" + filename3; // Ads.Image1 = Convert.ToString(filepath1); // Ads.Image2 = Convert.ToString(filepath2); // Ads.image3 = Convert.ToString(filepath3); // } // else if (filename1 == "" && filename2 != "" && filename3 != "") // { // file1.SaveAs(Server.MapPath("~/Content/Images/img1/" + DefaultImage)); // file2.SaveAs(Server.MapPath("~/Content/Images/img2/" + filename2)); // file3.SaveAs(Server.MapPath("~/Content/Images/img3/" + filename3)); // string filepath1 = "../../Content/Images/img1/" + DefaultImage; // string filepath2 = "../../Content/Images/img2/" + filename2; // string filepath3 = "../../Content/Images/img3/" + filename3; // Ads.Image1 = Convert.ToString(filepath1); // Ads.Image2 = Convert.ToString(filepath2); // Ads.image3 = Convert.ToString(filepath3); // } // else if (filename1 == "" && filename2 != "" && filename3 == "") // { // file1.SaveAs(Server.MapPath("~/Content/Images/img1/" + DefaultImage)); // file2.SaveAs(Server.MapPath("~/Content/Images/img2/" + filename2)); // file3.SaveAs(Server.MapPath("~/Content/Images/img3/" + DefaultImage)); // string filepath1 = "../../Content/Images/img1/" + DefaultImage; // string filepath2 = "../../Content/Images/img2/" + filename2; // string filepath3 = "../../Content/Images/img3/" + DefaultImage; // Ads.Image1 = Convert.ToString(filepath1); // Ads.Image2 = Convert.ToString(filepath2); // Ads.image3 = Convert.ToString(filepath3); // } // else if (filename1 == "" && filename2 == "" && filename3 != "") // { // file1.SaveAs(Server.MapPath("~/Content/Images/img1/" + DefaultImage)); // file2.SaveAs(Server.MapPath("~/Content/Images/img2/" + DefaultImage)); // file3.SaveAs(Server.MapPath("~/Content/Images/img3/" + filename3)); // string filepath1 = "../../Content/Images/img1/" + DefaultImage; // string filepath2 = "../../Content/Images/img2/" + DefaultImage; // string filepath3 = "../../Content/Images/img3/" + filename3; // Ads.Image1 = Convert.ToString(filepath1); // Ads.Image2 = Convert.ToString(filepath2); // Ads.image3 = Convert.ToString(filepath3); // } // else // { // file1.SaveAs(Server.MapPath("~/Content/Images/img1/" + filename1)); // file2.SaveAs(Server.MapPath("~/Content/Images/img2/" + DefaultImage)); // file3.SaveAs(Server.MapPath("~/Content/Images/img3/" + DefaultImage)); // string filepath1 = "../../Content/Images/img1/" + filename1; // string filepath2 = "../../Content/Images/img2/" + DefaultImage; // string filepath3 = "../../Content/Images/img3/" + DefaultImage; // Ads.Image1 = Convert.ToString(filepath1); // Ads.Image2 = Convert.ToString(filepath2); // Ads.image3 = Convert.ToString(filepath3); // } // } //} if (ModelState.IsValid) { _entities.Entry(Ads).State = EntityState.Modified; _entities.SaveChanges(); return RedirectToAction("UserPostDetails/" + Session["Id"]); } return View(Ads); }
public ActionResult AddPost(PostAd postads) { postads.IsHide = false; postads.PostedDate = DateTime.Now; if (postads.IsFeatured) postads.Status = "pending"; else postads.Status = "active"; HttpPostedFileBase file1 = Request.Files["file1"]; HttpPostedFileBase file2 = Request.Files["file2"]; HttpPostedFileBase file3 = Request.Files["file3"]; #region VariableDeclaration string filename1 = Path.GetFileName(file1.FileName); string filename2 = Path.GetFileName(file2.FileName); string filename3 = Path.GetFileName(file3.FileName); string DefaultImage1 = "Anon1.jpg"; string DefaultImage2 = "Anon2.jpg"; string DefaultImage3 = "Anon3.jpg"; string fileExtension1 = Path.GetExtension(file1.FileName); if (fileExtension1 == "") fileExtension1 = ".jpg"; string fileExtension2 = Path.GetExtension(file2.FileName); if (fileExtension2 == "") fileExtension2 = ".jpg"; string fileExtension3 = Path.GetExtension(file3.FileName); if (fileExtension3 == "") fileExtension3 = ".jpg"; int fileSize1 = file1.ContentLength; if (fileSize1 == 0) fileSize1 = 5995; int fileSize2 = file2.ContentLength; if (fileSize2 == 0) fileSize2 = 5995; int fileSize3 = file3.ContentLength; if (fileSize3 == 0) fileSize3 = 5995; #endregion if (ModelState.IsValid) { if (filename1 == "" && filename2 == "" && filename3 == "") { TempData["AllImageNull"] = "Please upload atleast one image of your Product"; } else { if (!(fileExtension1.ToLower() == ".jpg" || fileExtension1.ToLower() == ".jpeg" || fileExtension1.ToLower() == ".png")) { TempData["FileExtensionError"] = "Image(s) extension must be in .jpg OR .jpeg OR .png format "; } else if (!(fileExtension2.ToLower() == ".jpg" || fileExtension2.ToLower() == ".jpeg" || fileExtension2.ToLower() == ".png")) { TempData["FileExtensionError"] = "Image(s) extension must be in .jpg OR .jpeg OR .png format "; } else if (!(fileExtension3.ToLower() == ".jpg" || fileExtension3.ToLower() == ".jpeg" || fileExtension3.ToLower() == ".png")) { TempData["FileExtensionError"] = "Image(s) extension must be in .jpg OR .jpeg OR .png format "; } else if (fileSize1 > 102400 || fileSize2 > 102400 || fileSize3 > 102400) { TempData["FileSizeError"] = "Product image exceed the maximum size of 100KB."; } else { if (filename1 != "" && filename2 != "" && filename3 != "") { file1.SaveAs(Server.MapPath("~/Content/Images/img1/" + filename1)); file2.SaveAs(Server.MapPath("~/Content/Images/img2/" + filename2)); file3.SaveAs(Server.MapPath("~/Content/Images/img3/" + filename3)); string filepath1 = "../../Content/Images/img1/" + filename1; string filepath2 = "../../Content/Images/img2/" + filename2; string filepath3 = "../../Content/Images/img3/" + filename3; postads.Image1 = Convert.ToString(filepath1); postads.Image2 = Convert.ToString(filepath2); postads.Image3 = Convert.ToString(filepath3); } else if (filename1 == "" && filename2 != "" && filename3 != "") { file1.SaveAs(Server.MapPath("~/Content/Images/img1/" + DefaultImage1)); file2.SaveAs(Server.MapPath("~/Content/Images/img2/" + filename2)); file3.SaveAs(Server.MapPath("~/Content/Images/img3/" + filename3)); string filepath1 = "../../Content/Images/img1/" + DefaultImage1; string filepath2 = "../../Content/Images/img2/" + filename2; string filepath3 = "../../Content/Images/img3/" + filename3; postads.Image1 = Convert.ToString(filepath1); postads.Image2 = Convert.ToString(filepath2); postads.Image3 = Convert.ToString(filepath3); } else if (filename1 == "" && filename2 != "" && filename3 == "") { file1.SaveAs(Server.MapPath("~/Content/Images/img1/" + DefaultImage1)); file2.SaveAs(Server.MapPath("~/Content/Images/img2/" + filename2)); file3.SaveAs(Server.MapPath("~/Content/Images/img3/" + DefaultImage3)); string filepath1 = "../../Content/Images/img1/" + DefaultImage1; string filepath2 = "../../Content/Images/img2/" + filename2; string filepath3 = "../../Content/Images/img3/" + DefaultImage3; postads.Image1 = Convert.ToString(filepath1); postads.Image2 = Convert.ToString(filepath2); postads.Image3 = Convert.ToString(filepath3); } else if (filename1 == "" && filename2 == "" && filename3 != "") { file1.SaveAs(Server.MapPath("~/Content/Images/img1/" + DefaultImage1)); file2.SaveAs(Server.MapPath("~/Content/Images/img2/" + DefaultImage2)); file3.SaveAs(Server.MapPath("~/Content/Images/img3/" + filename3)); string filepath1 = "../../Content/Images/img1/" + DefaultImage1; string filepath2 = "../../Content/Images/img2/" + DefaultImage2; string filepath3 = "../../Content/Images/img3/" + filename3; postads.Image1 = Convert.ToString(filepath1); postads.Image2 = Convert.ToString(filepath2); postads.Image3 = Convert.ToString(filepath3); } else { file1.SaveAs(Server.MapPath("~/Content/Images/img1/" + filename1)); file2.SaveAs(Server.MapPath("~/Content/Images/img2/" + DefaultImage2)); file3.SaveAs(Server.MapPath("~/Content/Images/img3/" + DefaultImage3)); string filepath1 = "../../Content/Images/img1/" + filename1; string filepath2 = "../../Content/Images/img2/" + DefaultImage2; string filepath3 = "../../Content/Images/img3/" + DefaultImage3; postads.Image1 = Convert.ToString(filepath1); postads.Image2 = Convert.ToString(filepath2); postads.Image3 = Convert.ToString(filepath3); } try { _entities.PostAds.Add(postads); _entities.SaveChanges(); ModelState.Clear(); postads = null; TempData["SuccessMessage"] = "You have Posted your Ads Successfully"; } catch (Exception) { return RedirectToAction("DashBoardforNewPost"); } } } } return RedirectToAction("DashBoardforNewPost"); }