public ActionResult CreateProduct(HttpPostedFileBase ImgFile, Product cProduct, string base64str) { //驗證檔案 ImgUploadValidate ImgV = new ImgUploadValidate(); ImgV.filesize = 2000; //限制2mb if (ImgV.UploadUserFile(ImgFile)) { //檔案驗證成功 //local string fileName = cProduct.ProductID + ImgV.contentType; var path = Path.Combine(Server.MapPath("~/Image"), fileName); ImgFile.SaveAs(path); cProduct.ProductImg = "~/Image/" + fileName; //compress using (ImageMagick.MagickImage oImage = new ImageMagick.MagickImage(path)) { oImage.Format = MagickFormat.Jpg; oImage.ColorSpace = ImageMagick.ColorSpace.sRGB; //色盤採用sRGB oImage.Quality = 80; //壓縮率 if (oImage.Height < 200) { oImage.Resize(0, 200); } oImage.Strip(); //去除圖片profile oImage.Write(path); } //db byte[] FileBytes; using (MemoryStream ms = new MemoryStream()) { ImgFile.InputStream.CopyTo(ms); FileBytes = ms.GetBuffer(); } cProduct.ProductImg_DB = FileBytes; cProduct.Create_Date = DateTime.Now; cProduct.Delete_Flag = false; cProduct.Shelf_Flag = true; if (ModelState.IsValid) { //model驗證成功 if (db.Product.Any(p => p.ProductID.Equals(cProduct.ProductID))) //判斷資料是否重複 { TempData["DBResultErrorMessage"] = cProduct.ProductID + "資料已重複,請重新輸入"; return(View("CreateProduct")); } try { db.Product.Add(cProduct); db.SaveChanges(); } catch (Exception ex) {//資料庫異動例外狀況 logger.Error(ex.Message); TempData["DBResultErrorMessage"] = ex.Message; return(View("CreateProduct")); } TempData["DBResultErrorMessage"] = cProduct.ProductID + "新增成功!"; return(RedirectToAction("ProductList")); //新增成功 } else { //model 驗證失敗 return(View("CreateProduct")); } } else {//檔案驗證失敗 TempData["ImgValidate"] = ImgV.ErrorMessage; return(View("CreateProduct")); } }
public ActionResult ProductEdit(Product product, HttpPostedFileBase ImgFile) { var Product = db.Product.Where(m => m.ProductID == product.ProductID).FirstOrDefault(); //驗證檔案 ImgUploadValidate ImgV = new ImgUploadValidate(); ImgV.filesize = 2000; //限制2mb if (ImgV.UploadUserFile(ImgFile) || ImgV.ErrorMessage == "檔案不能為空的") { //檔案驗證成功 if (ImgV.UploadUserFile(ImgFile)) { //local string fileName = product.ProductID + ImgV.contentType; var path = Path.Combine(Server.MapPath("~/Image"), fileName); ImgFile.SaveAs(path); product.ProductImg = "~/Image/" + fileName; //compress using (ImageMagick.MagickImage oImage = new ImageMagick.MagickImage(path)) { oImage.Format = MagickFormat.Jpg; oImage.ColorSpace = ImageMagick.ColorSpace.sRGB; //色盤採用sRGB oImage.Quality = 80; //壓縮率 if (oImage.Height < 200) { oImage.Resize(0, 200); } oImage.Strip(); //去除圖片profile oImage.Write(path); } //db byte[] FileBytes; using (MemoryStream ms = new MemoryStream()) { ImgFile.InputStream.CopyTo(ms); FileBytes = ms.GetBuffer(); } Product.ProductImg_DB = FileBytes; Product.ProductImg = "~/Image/" + fileName; } Product.Modify_Date = DateTime.Now; Product.ProductExplain = product.ProductExplain; Product.ProductName = product.ProductName; Product.ProductPrice = product.ProductPrice; Product.Shelf_Flag = product.Shelf_Flag; Product.CategoryID = product.CategoryID; try { if (ModelState.IsValid) { //model驗證成功 db.SaveChanges(); TempData["EditMessage"] = product.ProductID + "更新成功!"; return(RedirectToAction("ProductList")); } else { //model驗證失敗 TempData["EditMessage"] = "更新失敗,請確認資料格式是否正確!!"; return(View(Product)); } } catch (Exception ex) { //資料庫例外狀況 logger.Error(ex.Message); TempData["EditMessage"] = ex.Message; return(View(Product)); } //return RedirectToAction("ProductList"); } else { //檔案驗證失敗 TempData["EditMessage"] = ImgV.ErrorMessage; return(View(Product)); } }