public ActionResult DeleteProduct(ProductVM productvm) { Product product = _productservice.GetProduct(productvm.Id); _productservice.DeleteProduct(productvm.Id); //delete files in productfolder - keep the product directory DirectoryInfo originalDirectory = new DirectoryInfo(string.Format("{0}Images\\Uploads", Server.MapPath(@"\"))); string pathStringProductsId = Path.Combine(originalDirectory.ToString(), "Products\\" + productvm.Id.ToString()); DirectoryInfo dirProducts = new DirectoryInfo(pathStringProductsId); foreach (FileInfo FileProducts in dirProducts.GetFiles()) { FileProducts.Delete(); } return(this.RedirectToAction <ShopController>(c => c.Products(1, null))); }
public ActionResult EditProduct(ProductVM productvm, HttpPostedFileBase file) { DirectoryInfo originalDirectory; Product product = _productservice.GetProduct(productvm.Id); productvm.Categories = new SelectList(_categoryservice.GetAllCategories(), "Id", "Name"); productvm.GalleryImages = Directory.EnumerateFiles(Server.MapPath("~/Images/Uploads/Products/" + productvm.Id + "/Gallery/Thumbs")). Select(fn => Path.GetFileName(fn)); if (!ModelState.IsValid) { return(View(productvm)); } else { product.Name = productvm.Name; product.Description = productvm.Description; product.Price = productvm.Price; product.category = productvm.category; if (file != null) { product.ImageName = file.FileName; } else { product.ImageName = productvm.ImageName; } #region fileupload if (file != null && file.ContentLength > 0) { if (file.ContentType.ToLower() == "image/jpg" || file.ContentType.ToLower() == "image/jpeg" || file.ContentType.ToLower() == "image/gif" || file.ContentType.ToLower() == "image/png") { originalDirectory = new DirectoryInfo(string.Format("{0}Images\\Uploads", Server.MapPath(@"\"))); string pathStringProducts = Path.Combine(originalDirectory.ToString(), "Products"); string pathStringThumbs = Path.Combine(originalDirectory.ToString(), "Products\\" + productvm.Id.ToString() + "\\Thumbs"); DirectoryInfo dirProducts = new DirectoryInfo(pathStringProducts); DirectoryInfo dirProductsThumbs = new DirectoryInfo(pathStringThumbs); foreach (FileInfo FileProducts in dirProducts.GetFiles()) { FileProducts.Delete(); } foreach (FileInfo FileThumbs in dirProductsThumbs.GetFiles()) { FileThumbs.Delete(); } } } _productservice.EditProduct(product); // Set original and thumb image paths originalDirectory = new DirectoryInfo(string.Format("{0}Images\\Uploads", Server.MapPath(@"\"))); string pathStringProductsId = Path.Combine(originalDirectory.ToString(), "Products\\" + productvm.Id.ToString()); if (file != null) { var path = string.Format("{0}\\{1}", pathStringProductsId, file.FileName); file.SaveAs(path); string pathStringThumbs2 = Path.Combine(originalDirectory.ToString(), "Products\\" + productvm.Id.ToString() + "\\Thumbs"); var path2 = string.Format("{0}\\{1}", pathStringThumbs2, file.FileName); // Create and save thumb WebImage img = new WebImage(file.InputStream); img.Resize(200, 200); img.Save(path2); #endregion } TempData["SM"] = "You have edited a product"; } return(this.RedirectToAction <ShopController>(c => c.Products(1, null))); }