public async Task <ActionResult> Update() { var model = new ProductAppIG4Item(); UpdateModel(model); var product = _productDa.GetById(model.ID); if (product.CustomerID != CustomerId) { return(Json(new JsonMessage(4001, "Bạn không có quyển xóa sản phẩm này"))); } var files = Request.Files; if (files.Count == 0 && model.LstPictures != null && model.LstPictures.Count() >= product.Shop_Product_Picture.Count()) { return(Json(new JsonMessage(1000, "Ảnh sản phẩn không được để trống."))); } var date = DateTime.Now; var folder = date.Year + "\\" + date.Month + "\\" + date.Day + "\\"; var fileinsert = date.Year + "/" + date.Month + "/" + date.Day + "/"; var urlFolder = ConfigData.OriginalFolder; ImageProcess.CreateForder(ConfigData.OriginalFolder); ImageProcess.CreateForder(ConfigData.ThumbsFolder); ImageProcess.CreateForder(ConfigData.MediumsFolder); List <Gallery_Picture> images = new List <Gallery_Picture>(); for (int i = 0; i < Request.Files.Count; i++) { var img = await UploadImage(i); if (img.Code != 200) { return(Json(img)); } var picture = new Gallery_Picture { Type = !string.IsNullOrEmpty(Request["type"]) ? Convert.ToInt32(Request["type"]) : 0, CategoryID = model.CateId, Folder = img.Data.Folder, Name = img.Data.Name, DateCreated = DateTime.Now.TotalSeconds(), IsShow = true, Url = img.Data.Url, IsDeleted = false, }; images.Add(picture); _da.Add(picture); } await _da.SaveAsync(); if (model.AddressId != product.AddressId) { //lay address var address = customerAddressDA.GetById(model.AddressId, CustomerId); if (address == null) { return(Json(new JsonMessage(1000, "Địa chỉ không tồn tại."))); } product.AddressId = model.AddressId; product.Latitude = address.Latitude.Value; product.Longitude = address.Longitude.Value; } product.Name = model.Name; product.Quantity = model.Quantity; product.Description = model.Description; product.PriceNew = model.PriceNew; product.CustomerID = CustomerId; product.HasTransfer = model.HasTransfer; product.Type = model.Type; product.CategoryId = model.CateId.Value; product.CustomerID1 = model.CustomerId1; var idImages = product.Shop_Product_Picture.Where(m => model.PictureDeleteIds == null || !model.PictureDeleteIds.Any(n => n == m.Gallery_Picture.ID)).Select(m => m.Gallery_Picture.ID).ToList(); idImages.AddRange(images.Select(m => m.ID).ToList()); //product.Shop_Product_Picture = _productDa.GetListPictureByArrId(idImages); //if (!product.Categories.Any(m => m.Id == model.CateId)) //{ // product.Categories.Clear(); // product.Categories = _productDa.GetListCategoryByArrId(new List<int>() { model.CateId.Value }); //} await _productDa.SaveAsync(); return(Json(new JsonMessage(200, ""), JsonRequestBehavior.AllowGet)); }
public ActionResult AjaxFormPictureSubmit() { var date = DateTime.Now; var msg = new JsonMessage(false, "Thêm mới hình ảnh thành công."); var folder = date.Year + "\\" + date.Month + "\\" + date.Day + "\\"; var fileinsert = date.Year + "/" + date.Month + "/" + date.Day + "/"; var folderinsert = fileinsert; var urlFolder = ConfigData.TempFolder; var lstFile = Request["lstFile"]; var lstP = JsonConvert.DeserializeObject <List <FileUploadItem> >(lstFile); try { foreach (var item in lstP) { var fileName = item.Url; var imageSource = Image.FromFile(urlFolder + fileName); var checkfolder = false; if (Request["ckImage_" + (int)FolderImage.Originals] != null) { checkfolder = true; ImageProcess.CreateForder(ConfigData.OriginalFolder); // tạo forder Năm / Tháng / Ngày if (imageSource.Width > ConfigData.ImageFullHdFile.Width) { var image = ImageProcess.ResizeImage(imageSource, ConfigData.ImageFullHdFile); ImageProcess.SaveJpeg(ConfigData.OriginalFolder + folder + fileName, new Bitmap(image), 92L); // Save file Original } else { System.IO.File.Copy(urlFolder + fileName, ConfigData.OriginalFolder + folder + fileName); } folderinsert = "Originals/" + fileinsert; } if (Request["ckImage_" + (int)FolderImage.Images] != null) { checkfolder = true; ImageProcess.CreateForder(ConfigData.ImageFolder); // tạo forder Năm / Tháng / Ngày if (imageSource.Width > ConfigData.ImageHdFile.Width) { var image = ImageProcess.ResizeImage(imageSource, ConfigData.ImageHdFile); ImageProcess.SaveJpeg(ConfigData.ImageFolder + folder + fileName, new Bitmap(image), 92L); // Save file Images } else { System.IO.File.Copy(urlFolder + fileName, ConfigData.ImageFolder + folder + fileName); } folderinsert = "Images/" + fileinsert; } //Resize ảnh 640 if (Request["ckImage_" + (int)FolderImage.Mediums] != null) { checkfolder = true; ImageProcess.CreateForder(ConfigData.ImageUploadMediumFolder); // tạo forder Năm / Tháng / Ngày if (imageSource.Width > ConfigData.ImageMediumFile.Width) { var image = ImageProcess.ResizeImage(imageSource, ConfigData.ImageFullHdFile); ImageProcess.SaveJpeg(ConfigData.ImageUploadMediumFolder + folder + fileName, new Bitmap(image), 92L); // Save file Medium } else { System.IO.File.Copy(urlFolder + fileName, ConfigData.ImageUploadMediumFolder + folder + fileName); } folderinsert = "Mediums/" + fileinsert; } if (!checkfolder) { folderinsert = "Thumbs/" + fileinsert; } if (Request["ckImage_" + (int)FolderImage.Thumbs] != null) { ImageProcess.CreateForder(ConfigData.ThumbsFolder); } if (imageSource.Width < ConfigData.ImageThumbsSize.Width) { ImageProcess.SaveJpeg(ConfigData.ThumbsFolder + folder + fileName, new Bitmap(imageSource), 92L); // Save file Thumbs } else { imageSource = ImageProcess.ResizeImage(imageSource, ConfigData.ImageThumbsSize); ImageProcess.SaveJpeg(ConfigData.ThumbsFolder + folder + fileName, new Bitmap(imageSource), 92L); // Save file Thumbs } imageSource.Dispose(); //Lấy thông tin cần thiết var picture = new Gallery_Picture { Type = !string.IsNullOrEmpty(Request["type"]) ? Convert.ToInt32(Request["type"]) : 0, CategoryID = !string.IsNullOrEmpty(Request["CategoryID"]) ? Convert.ToInt32(Request["CategoryID"]) : 1, Folder = folderinsert, Name = item.Name, LanguageId = Fdisystem.LanguageId, //AgencyId = Utility.AgencyId, DateCreated = DateTime.Now.TotalSeconds(), IsShow = true, Url = fileName, IsDeleted = false }; _da.Add(picture); } _da.Save(); try { var di = new DirectoryInfo(urlFolder); foreach (var file in di.GetFiles()) { file.Delete(); } foreach (var dir in di.GetDirectories()) { dir.Delete(true); } } catch { } } catch (Exception ex) { msg.Erros = true; msg.Message = "Thêm mới hình ảnh thất bại."; } return(Json(msg, JsonRequestBehavior.AllowGet)); }
public ActionResult AjaxFormPictureSubmit() { var date = DateTime.Now; var msg = new JsonMessage { Erros = false }; var intTotalFile = Convert.ToInt32(Request["NumberOfImage"]); var folder = date.Year + "\\" + date.Month + "\\" + date.Day + "\\"; var fileinsert = date.Year + "/" + date.Month + "/" + date.Day + "/"; var folderinsert = fileinsert; for (var idx = 0; idx < intTotalFile; idx++) { var fileNameLocal = Request["ImageFile_" + idx + ""]; var file = fileNameLocal.Split('.'); var nameslug = FomatString.Slug(file[0]); var fileName = nameslug + "-" + date.ToString("HHmmss") + "." + file[1]; if (!nameslug.Contains(ConfigData.WebTitle)) { fileName = ConfigData.WebTitle + nameslug + "-" + date.ToString("HHmmss") + "." + file[1]; } var fileTemp = nameslug + "." + file[1]; var imageSource = Image.FromFile(ConfigData.TempFolder + fileTemp); var checkfolder = false; if (Request["ckImage_" + (int)FolderImage.Originals] != null) { checkfolder = true; ImageProcess.CreateForder(ConfigData.OriginalFolder); // tạo forder Năm / Tháng / Ngày if (imageSource.Width > ConfigData.ImageFullHdFile.Width) { var image = ImageProcess.ResizeImage(imageSource, ConfigData.ImageFullHdFile); ImageProcess.SaveJpeg(ConfigData.OriginalFolder + folder + fileName, new Bitmap(image), 92L); // Save file Original } else { System.IO.File.Copy(ConfigData.TempFolder + fileTemp, ConfigData.OriginalFolder + folder + fileName); } folderinsert = "Originals/" + fileinsert; } if (Request["ckImage_" + (int)FolderImage.Images] != null) { checkfolder = true; ImageProcess.CreateForder(ConfigData.ImageFolder); // tạo forder Năm / Tháng / Ngày if (imageSource.Width > ConfigData.ImageHdFile.Width) { var image = ImageProcess.ResizeImage(imageSource, ConfigData.ImageHdFile); ImageProcess.SaveJpeg(ConfigData.ImageFolder + folder + fileName, new Bitmap(image), 92L); // Save file Images } else { System.IO.File.Copy(ConfigData.TempFolder + fileTemp, ConfigData.ImageFolder + folder + fileName); } folderinsert = "Images/" + fileinsert; } //Resize ảnh 640 if (Request["ckImage_" + (int)FolderImage.Mediums] != null) { checkfolder = true; ImageProcess.CreateForder(ConfigData.ImageUploadMediumFolder); // tạo forder Năm / Tháng / Ngày if (imageSource.Width > ConfigData.ImageMediumFile.Width) { var image = ImageProcess.ResizeImage(imageSource, ConfigData.ImageFullHdFile); ImageProcess.SaveJpeg(ConfigData.ImageUploadMediumFolder + folder + fileName, new Bitmap(image), 92L); // Save file Medium } else { System.IO.File.Copy(ConfigData.TempFolder + fileTemp, ConfigData.ImageUploadMediumFolder + folder + fileName); } folderinsert = "Mediums/" + fileinsert; } if (!checkfolder) { folderinsert = "Thumbs/" + fileinsert; } if (Request["ckImage_" + (int)FolderImage.Thumbs] != null) { ImageProcess.CreateForder(ConfigData.ThumbsFolder); } if (imageSource.Width < ConfigData.ImageThumbsSize.Width) { ImageProcess.SaveJpeg(ConfigData.ThumbsFolder + folder + fileName, new Bitmap(imageSource), 92L); // Save file Thumbs } else { imageSource = ImageProcess.ResizeImage(imageSource, ConfigData.ImageThumbsSize); ImageProcess.SaveJpeg(ConfigData.ThumbsFolder + folder + fileName, new Bitmap(imageSource), 92L); // Save file Thumbs } imageSource.Dispose(); //Lấy thông tin cần thiết var picture = new Gallery_Picture { Type = !string.IsNullOrEmpty(Request["type"]) ? Convert.ToInt32(Request["type"]) : 0, //CategoryID = // !string.IsNullOrEmpty(Request["CategoryID"]) ? Convert.ToInt32(Request["CategoryID"]) : 1, LanguageId = Fdisystem.LanguageId, DateCreated = date.TotalSeconds(), Folder = folderinsert, Name = Request["ImageName_" + idx], IsShow = true, Url = fileName, IsDeleted = false, CreateBy = User.Identity.Name, UpdateBy = User.Identity.Name }; _pictureDa.Add(picture); msg.Message += string.Format("Đã thêm hình ảnh: <b>{0}</b><br/>", picture.Name); } _pictureDa.Save(); return(Json(msg, JsonRequestBehavior.AllowGet)); }