public ActionResult Create([Bind(Include = "ProductCode,CategoryID,Title,Quantity,Unit,PriceOfUnit,CoverImageID,Description,Description2,TotalView,TotalBuy,Tags,IsNewProduct,IsBestSellProduct,SortOrder,Status,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate")] product_Products product_Products, HttpPostedFileBase upload) { if (ModelState.IsValid) { HttpPostedFileBase file = upload; if (upload != null && upload.ContentLength > 0) { if (file.ContentLength > 0) { // width + height will force size, care for distortion //Exmaple: ImageUpload imageUpload = new ImageUpload { Width = 800, Height = 700 }; // height will increase the width proportionally //Example: ImageUpload imageUpload = new ImageUpload { Height= 600 }; // width will increase the height proportionally ImageUpload imageUpload = new ImageUpload { Width = 600 }; // rename, resize, and upload //return object that contains {bool Success,string ErrorMessage,string ImageName} ImageResult imageResult = imageUpload.RenameUploadFile(file); if (imageResult.Success) { //TODO: write the filename to the db var photo = new share_Images { ImageName = imageResult.ImageName, ImagePath = Path.Combine(ImageUpload.LoadPath, imageResult.ImageName) }; product_Products.share_Images = new List<share_Images>(); product_Products.share_Images.Add(photo); if (product_Products.share_Images.Count() > 0) { product_Products.CoverImageID = product_Products.share_Images.ElementAt(0).ID; } } else { // use imageResult.ErrorMessage to show the error ViewBag.Error = imageResult.ErrorMessage; } } } product_Products.GUID = System.Guid.NewGuid(); db.product_Products.Add(product_Products); db.SaveChanges(); return RedirectToAction("Index"); } PopulateStatusDropDownList(product_Products.Status); ViewBag.CategoryID = new SelectList(db.product_Categories, "ID", "Title", product_Products.CategoryID); return View(product_Products); }
public ActionResult UploadImage(IEnumerable<HttpPostedFileBase> files, int? IdProduct) { if (IdProduct == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } product_Products product_Products = db.product_Products .Include(i => i.product_Categories) .Include(i => i.share_Images) .Where(i => i.ID == IdProduct) .Single(); if (files != null) { foreach (var file in files) { if (file.ContentLength > 0) { // width + height will force size, care for distortion //Exmaple: ImageUpload imageUpload = new ImageUpload { Width = 800, Height = 700 }; // height will increase the width proportionally //Example: ImageUpload imageUpload = new ImageUpload { Height= 600 }; // width will increase the height proportionally ImageUpload imageUpload = new ImageUpload { Width = 600 }; // rename, resize, and upload //return object that contains {bool Success,string ErrorMessage,string ImageName} ImageResult imageResult = imageUpload.RenameUploadFile(file); if (imageResult.Success) { //TODO: write the filename to the db var photo = new share_Images { ImageName = imageResult.ImageName, ImagePath = Path.Combine(ImageUpload.LoadPath, imageResult.ImageName) }; if (product_Products.share_Images == null) { product_Products.share_Images = new List<share_Images>(); } product_Products.share_Images.Add(photo); if (product_Products.share_Images.Count() > 0) { product_Products.CoverImageID = product_Products.share_Images.ElementAt(0).ID; } } else { // use imageResult.ErrorMessage to show the error ViewBag.Error = imageResult.ErrorMessage; } } } db.SaveChanges(); } LoadListImageProductPartialViewModels listImageViewModels = new LoadListImageProductPartialViewModels() { ProductId = product_Products.ID, Images = product_Products.share_Images }; return PartialView("LoadListImageProduct", listImageViewModels); }