//[ValidateAntiForgeryToken] public ActionResult Uploadgallery(HttpPostedFileBase[] files) { if (files[0] != null && files.Any()) { string names = ""; foreach (HttpPostedFileBase file in files) { //--------------------Creating names and saving to Main sarver---------------------------------- string galleryname = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); file.SaveAs(Server.MapPath("/Content/Upload/productImages/Images/" + galleryname)); //---------------------resize Images ----------------------------------------------------------- InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(300); img.Resize(Server.MapPath("/Content/Upload/productImages/Images/") + galleryname, Server.MapPath("/Content/Upload/productImages/thumbnail/") + galleryname); //----------------------upload via ftp to download server -------------------------------------- //FtpHelper.Upload(Server.MapPath("/Content/Upload/productImages/Images/") + galleryname, "ProductImages/Images/"); //FtpHelper.Upload(Server.MapPath("/Content/Upload/productImages/thumbnail/") + galleryname, "ProductImages/thumbnail/"); //-----------------------deleting from MAin server---------------------------------------------- //if ((System.IO.File.Exists(Server.MapPath("/Content/Upload/productImages/Images/") + galleryname))) // System.IO.File.Delete(Server.MapPath("/Content/Upload/productImages/Images/") + galleryname); names += galleryname + ","; } //saving new store and the images return(Json(new { status = "Done", ImagesName = names })); } return(Json(new { status = "Error" })); }
public async Task <IActionResult> Create(User user, IFormFile file) { var fileName = "no-photo.png"; if (ModelState.IsValid) { if (file != null) { var uploads = Path.Combine(_env.WebRootPath, "Upload\\Profile"); fileName = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); using (var fileStream = new FileStream(Path.Combine(uploads, fileName), FileMode.Create)) { await file.CopyToAsync(fileStream); } //---------------------resize Images ---------------------- InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(128); img.Resize(Path.Combine(uploads, fileName), Path.Combine(uploads, "thumbnail", fileName)); } } user.Profile = fileName; //--------------------------- await _userService.Add(user); ViewData["RoleId"] = new SelectList(await _roleService.Roles(), "RoleId", "RoleName", user.RoleId); ViewData["chartPostId"] = new SelectList(await _chartPost.chartPosts(), "chartPostId", "Postduty", user.chartPostId); return(RedirectToAction(nameof(Index))); }
//[ValidateAntiForgeryToken] public ActionResult UploadFile(HttpPostedFileBase file) { if (file != null) { string imagename = "no-photo.jpg"; if (file != null) { //--------------------Creating names and saving to Main sarver---------------------------------- imagename = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); file.SaveAs(Server.MapPath("/Content/Upload/productImages/Images/") + imagename); //---------------------resize Images ----------------------------------------------------------- InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(350); img.Resize(Server.MapPath("/Content/Upload/productImages/Images/") + imagename, Server.MapPath("/Content/Upload/productImages/thumbnail/") + imagename); //----------------------upload via ftp to download server -------------------------------------- //FtpHelper.Upload(Server.MapPath("/Content/Upload/productImages/Images/") + imagename, "ProductImages/Images/" ); //FtpHelper.Upload(Server.MapPath("/Content/Upload/productImages/thumbnail/") + imagename, "ProductImages/thumbnail/"); //-----------------------deleting from MAin server---------------------------------------------- //if ((System.IO.File.Exists(Server.MapPath("/Content/Upload/productImages/Images/") + imagename))) // System.IO.File.Delete(Server.MapPath("/Content/Upload/productImages/Images/") + imagename); } //saving new store and the images return(Json(new { status = "Done", src = "/Content/Upload/productImages/thumbnail/" + imagename, ImageName = imagename })); } return(Json(new { status = "Error" })); }
public ActionResult Edit(User user, HttpPostedFileBase file) { if (ModelState.IsValid) { if (file != null) { if (user.Profile != "no-photo.png") { if (System.IO.File.Exists(Server.MapPath("/Content/Upload/Profile/" + user.Profile))) { System.IO.File.Delete(Server.MapPath("/Content/Upload/Profile/" + user.Profile)); } if (System.IO.File.Exists(Server.MapPath("/Content/Upload/Profile/thumbnail/" + user.Profile))) { System.IO.File.Delete(Server.MapPath("/Content/Upload/Profile/thumbnail/" + user.Profile)); } } user.Profile = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); file.SaveAs(Server.MapPath("/Content/Upload/Profile/" + user.Profile)); InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(150); img.Resize(Server.MapPath("/Content/Upload/Profile/" + user.Profile), Server.MapPath("/Content/Upload/Profile/thumbnail/" + user.Profile)); } _userService.Edit(user); return(RedirectToAction("Index")); } ViewBag.RoleID = new SelectList(_roleService.Roles(), "RoleId", "RoleName", user.RoleId); return(View(user)); }
public async Task <IActionResult> AddEditSlider(int id, Slider model, IEnumerable <IFormFile> files, string Imgename) { if (ModelState.IsValid) { var uploud = Path.Combine(_appEnvironmen.WebRootPath, "upload\\normalimage\\"); foreach (var file in files) { if (file != null && file.Length > 0) { var filename = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); using (var fs = new FileStream(Path.Combine(uploud, filename), FileMode.Create)) { await file.CopyToAsync(fs); model.Image = filename; } InsertShowImage.ImageResizer image = new InsertShowImage.ImageResizer(); image.Resize(uploud + filename, _appEnvironmen.WebRootPath + "\\upload\\thumbnailimage\\" + filename); } } if (id == 0) { if (model.Image == null) { model.Image = "defaultpic.png"; } using (var db = _IServiceProvider.GetRequiredService <ApplicationDbContext>()) { db.Sliders.Add(model); await db.SaveChangesAsync(); } TempData["Notif"] = Notification.ShowNotif(MessageType.Add, type: ToastType.green); return(Json(new { Status = "success" })); } else { if (model.Image == null) { model.Image = Imgename; } using (var db = _IServiceProvider.GetRequiredService <ApplicationDbContext>()) { db.Sliders.Update(model); await db.SaveChangesAsync(); } TempData["Notif"] = Notification.ShowNotif(MessageType.Edit, type: ToastType.blue); return(Json(new { Status = "success" })); } } var list = new List <string>(); foreach (var validation in ViewData.ModelState.Values) { list.AddRange(validation.Errors.Select(error => error.ErrorMessage)); } return(Json(new { Status = "error" })); }
public async Task <IActionResult> AddEditNews(int NewsId, News model, IEnumerable <IFormFile> files, string ImgName) { ViewBag.RootPath = "/upload/thumbnailnews/"; if (ModelState.IsValid) { #region Begin: upload image var uploads = Path.Combine(_appEnvironment.WebRootPath, "upload\\normalnews\\"); var uploadsThubnail = Path.Combine(_appEnvironment.WebRootPath, "upload\\thumbnailnews\\"); foreach (var file in files) { if (file != null && file.Length > 0) { var filename = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); using (var fs = new FileStream(Path.Combine(uploads, filename), FileMode.Create)) { await file.CopyToAsync(fs); model.newsImage = filename; } // resize image InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(); img.Resize(uploads + filename, uploadsThubnail + filename); } } #endregion if (NewsId == 0) { // insert _ins.create(model); _ins.save(); return(Json(new { status = "success", message = "اطلاعیه یا خبر با موفقیت ایجاد شد" })); } else { // update if (model.newsImage == null) { model.newsImage = ImgName; } _ins.update(model); _ins.save(); return(Json(new { status = "success", message = "اطلاعیه و خبر با موفقیت بروز رسانی شد" })); } } #region Display validation with jquery ajax var list = new List <string>(); foreach (var validation in ViewData.ModelState.Values) { list.AddRange(validation.Errors.Select(error => error.ErrorMessage)); } #endregion return(Json(new { status = "error", error = list })); }
public ActionResult Create([Bind(Include = "ProductID,GroupID,ProductTitle,ProductDescription,ProductPrice,ProductImage,CreateDate")] Products products, HttpPostedFileBase ProductImage, HttpPostedFileBase[] ProductGallery, string tags) { if (ModelState.IsValid) { string imagename = "no-photo.jpg"; if (ProductImage != null) { imagename = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(ProductImage.FileName); ProductImage.SaveAs(Server.MapPath("/ProductImages/Image/" + imagename)); //----resize---- InsertShowImage.ImageResizer imag = new InsertShowImage.ImageResizer(); imag.Resize(Server.MapPath("/ProductImages/Image/" + imagename), Server.MapPath("/ProductImages/Thumb/" + imagename)); } products.ProductImage = imagename; products.CreateDate = DateTime.Now; db.Products.Add(products); //-----creae gallery--- if (ProductGallery != null && ProductGallery.Any()) { foreach (HttpPostedFileBase file in ProductGallery) { string galleryname = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); file.SaveAs(Server.MapPath("/ProductImages/Image/" + galleryname)); //----resiza gallery--- InsertShowImage.ImageResizer imag = new InsertShowImage.ImageResizer(); imag.Resize(Server.MapPath("/ProductImages/Image/" + galleryname), Server.MapPath("/ProductImages/Thumb/" + galleryname)); db.Product_Galleries.Add(new Product_Galleries() { ImageName = galleryname, ProductID = products.ProductID, }); } } //---------------tags------- if (!string.IsNullOrEmpty(tags)) { string[] tag = tags.Split('-'); foreach (string t in tag) { db.Product_Tags.Add(new Product_Tags() { ProductID = products.ProductID, TagTitle = t.Trim() }); } } db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.GroupID = new SelectList(db.Product_Groups, "GroupID", "GroupTitle", products.GroupID); return(View(products)); }
public ActionResult Create(News news, HttpPostedFileBase NewsImage, HttpPostedFileBase[] newsGalleryPost, string tags) { if (ModelState.IsValid) { news.UserId = _userService.GetUserByIdentity(User.Identity.Name).UserId; news.NewsImage = "no-photo.jpg"; if (NewsImage != null) { news.NewsImage = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(NewsImage.FileName); NewsImage.SaveAs(Server.MapPath("/Content/Upload/NewsImages/Images/") + news.NewsImage); //---------------------resize Images ---------------------- InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(150); img.Resize(Server.MapPath("/Content/Upload/NewsImages/Images/") + news.NewsImage, Server.MapPath("/Content/Upload/NewsImages/thumbnail/") + news.NewsImage); } //------------Create Gallery Product -------------- if (newsGalleryPost[0] != null && newsGalleryPost.Any()) { foreach (HttpPostedFileBase file in newsGalleryPost) { string galleryname = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); file.SaveAs(Server.MapPath("/Content/Upload/NewsImages/Images/" + galleryname)); //---------------------resize Images for Gallery---------------------- InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(350); img.Resize(Server.MapPath("/Content/Upload/NewsImages/Images/") + galleryname, Server.MapPath("/Content/Upload/NewsImages/thumbnail/") + galleryname); news.NewsGallery.Add(new NewsGallery() { NewsId = news.NewsId, ImageName = galleryname }); } } //-------------------Tags--------------------- if (!string.IsNullOrEmpty(tags)) { string[] tag = tags.Split('-'); foreach (string t in tag) { news.NewsTag.Add(new NewsTag() { NewsId = news.NewsId, TagsTitle = t.Trim() }); } } _newsService.Add(news); return(RedirectToAction("Index")); } ViewBag.NewsGroupId = new SelectList(_newsGroupService.NewsGroups(), "NewsGroupId", "GroupTitle", news.NewsGroupId); return(View(news)); }
public async Task <IActionResult> Edit(int id, User user, IFormFile file) { if (id != user.UserId) { return(NotFound()); } if (ModelState.IsValid) { try { var fileName = "no-photo.png"; if (file != null) { var uploads = Path.Combine(_env.WebRootPath, "Upload\\Profile"); fileName = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); using (var fileStream = new FileStream(Path.Combine(uploads, fileName), FileMode.Create)) { await file.CopyToAsync(fileStream); } //---------------------resize Images ---------------------- InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(128); img.Resize(Path.Combine(uploads, fileName), Path.Combine(uploads, "thumbnail", fileName)); } user.Profile = fileName; await _userService.Edit(user); } catch (DbUpdateConcurrencyException) { if (await _userService.UserExistence(user.UserId) == null) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["RoleId"] = new SelectList(await _roleService.Roles(), "RoleId", "RoleName", user.RoleId); ViewData["chartPostId"] = new SelectList(await _chartPost.chartPosts(), "chartPostId", "Postduty", user.chartPostId); return(View(user)); }
//[ValidateAntiForgeryToken] public async Task <IActionResult> UploadGallery(List <IFormFile> files) { string names = ""; var uploads = Path.Combine(_env.WebRootPath, "Upload\\ProductImages"); foreach (var file in files) { string galleryname = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); using (var fileStream = new FileStream(Path.Combine(uploads, galleryname), FileMode.Create)) { await file.CopyToAsync(fileStream); } names += galleryname + ","; //---------------------resize Images ---------------------- InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(350); img.Resize(Path.Combine(uploads, galleryname), Path.Combine(uploads, "thumbnail", galleryname)); } return(Json(new { status = "Done", imagesName = names })); }
public async Task <IActionResult> UploadFile(IEnumerable <IFormFile> files) { var upload = Path.Combine(_appEnvironment.WebRootPath, "upload\\userimage\\normalimage\\"); var filename = ""; foreach (var file in files) { filename = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); using (var fs = new FileStream(Path.Combine(upload, filename), FileMode.Create)) { await file.CopyToAsync(fs); } } /////////تغییر سایز عکس و ذخیره InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(); img.Resize(upload + filename, _appEnvironment.WebRootPath + "\\upload\\userimage\\thumbnailimage\\" + filename); return(Json(new { status = "success", message = "تصویر با موفقیت آپلود شد.", imagename = filename })); }
//[ValidateAntiForgeryToken] public ActionResult UploadFile(HttpPostedFileBase file) { if (file != null) { string imagename = "no-photo.jpg"; if (file != null) { //--------------------Creating names and saving to Main sarver---------------------------------- imagename = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); file.SaveAs(Server.MapPath("/Content/Modules/Images/") + imagename); //---------------------resize Images ----------------------------------------------------------- InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(350); img.Resize(Server.MapPath("/Content/Modules/Images/") + imagename, Server.MapPath("/Content/Modules/Images/thumbnail/") + imagename); } //saving new store and the images return(Json(new { status = "Done", src = "/Content/Modules/Images/thumbnail/" + imagename, ImageName = imagename })); } return(Json(new { status = "Error" })); }
public string UploadFiles(IEnumerable <IFormFile> files, string uploadPath, string uploadthumbnailPath) { //"upload\\userimage\\normalimage\\" var upload = Path.Combine(_appEnvironment.WebRootPath, uploadPath); var filename = ""; foreach (var file in files) { filename = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); using (var fs = new FileStream(Path.Combine(upload, filename), FileMode.Create)) { file.CopyTo(fs); } } //"\\upload\\userimage\\thumbnailimage\\" /////////تغییر سایز عکس و ذخیره if (uploadthumbnailPath != "") { InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(); img.Resize(upload + filename, _appEnvironment.WebRootPath + uploadthumbnailPath + filename); } return(filename); }
public ActionResult Create(User user, HttpPostedFileBase file) { if (ModelState.IsValid) { string imagename = "no-photo.png"; if (file != null) { imagename = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); file.SaveAs(Server.MapPath("/Content/Upload/Profile/") + imagename); //---------------------resize Images ---------------------- InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(48); img.Resize(Server.MapPath("/Content/Upload/Profile/") + imagename, Server.MapPath("/Content/Upload/Profile/thumbnail/") + imagename); } user.Profile = imagename; //--------------------------- user.ActiveCode = Guid.NewGuid().ToString().Replace("-", ""); user.AddedDate = DateTime.Now; _userService.Add(user); return(RedirectToAction("Index")); } ViewBag.RoleID = new SelectList(_roleService.Roles(), "RoleId", "RoleName", user.RoleId); return(View(user)); }
//[ValidateAntiForgeryToken] public async Task <IActionResult> UploadFile(IFormFile file) { if (file != null) { string fileName = "no-photo.png"; var uploads = Path.Combine(_env.WebRootPath, "Upload\\ProductImages"); if (file != null) { fileName = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); using (var fileStream = new FileStream(Path.Combine(uploads, fileName), FileMode.Create)) { await file.CopyToAsync(fileStream); } //---------------------resize Images ---------------------- InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(128); img.Resize(Path.Combine(uploads, fileName), Path.Combine(uploads, "thumbnail", fileName)); } //saving new store and the images return(Json(new { status = "Done", src = Path.Combine("\\Upload\\ProductImages", fileName), ImageName = fileName })); } return(Json(new { status = "Error" })); }
public ActionResult Edit(News news, HttpPostedFileBase newsImages, HttpPostedFileBase[] newsGalleryPost, string tags) { if (ModelState.IsValid) { news.UserId = _userService.GetUserByIdentity(User.Identity.Name).UserId; if (newsImages != null) { if (news.NewsImage != "no-photo.jpg") { if (System.IO.File.Exists(Server.MapPath("/Content/Upload/NewsImages/Images/" + news.NewsImage))) { System.IO.File.Delete(Server.MapPath("/Content/Upload/NewsImages/Images/" + news.NewsImage)); } if (System.IO.File.Exists(Server.MapPath("/Content/Upload/NewsImages/thumbnail/" + news.NewsImage))) { System.IO.File.Delete(Server.MapPath("/Content/Upload/NewsImages/thumbnail/" + news.NewsImage)); } } news.NewsImage = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(newsImages.FileName); newsImages.SaveAs(Server.MapPath("/Content/Upload/NewsImages/Images/" + news.NewsImage)); InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(150); img.Resize(Server.MapPath("/Content/Upload/NewsImages/Images/" + news.NewsImage), Server.MapPath("/NewsImages/Thumbnail/" + news.NewsImage)); } //------------Create Gallery Product -------------- if (newsGalleryPost[0] != null && newsGalleryPost.Any()) { List <NewsGallery> newsGalleries = new List <NewsGallery>(); foreach (HttpPostedFileBase file in newsGalleryPost) { string galleryname = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); file.SaveAs(Server.MapPath("/Content/Upload/NewsImages/Images/" + galleryname)); //---------------------resize Images for Gallery---------------------- InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(350); img.Resize(Server.MapPath("/Content/Upload/NewsImages/Images/") + galleryname, Server.MapPath("/NewsImages/thumbnail/") + galleryname); newsGalleries.Add(new NewsGallery() { NewsId = news.NewsId, ImageName = galleryname }); } _newsService.AddGallery(newsGalleries); } //-------------------Tags--------------------- _newsService.DeleteTagsByNews(news.NewsId); if (!string.IsNullOrEmpty(tags)) { List <NewsTag> newsTag = new List <NewsTag>(); foreach (string tag in tags.Split('-')) { newsTag.Add(new NewsTag() { NewsId = news.NewsId, TagsTitle = tag.Trim() }); } _newsService.AddTags(newsTag); } news.ModifiedDate = DateTime.Now; _newsService.Edit(news); return(RedirectToAction("Index")); } ViewBag.NewsGroupId = new SelectList(_newsGroupService.NewsGroups(), "NewsGroupId", "GroupsTitle", news.NewsGroupId); return(View(news)); }
// [ValidateAntiForgeryToken] // this statement does'nt allow run ajax-jquery public async Task <IActionResult> AddEditBook(int bookId, AddEditBookViewModel model, IEnumerable <IFormFile> files, string imgName) { if (ModelState.IsValid) { //------################### *** Upload Image *** ------################### var uploads = Path.Combine(_appEnvironment.WebRootPath, "upload\\normalimage\\"); foreach (var item in files) { if (item != null && item.Length > 0) { //creating a unique name for each file and then atach the format of each file to the unique name var filename = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(item.FileName); //for saving path of file in data base and saving file in root project(wwwroot) using (var fs = new FileStream(Path.Combine(uploads, filename), FileMode.Create)) { await item.CopyToAsync(fs); model.BookImage = filename; } //---------------------------resize Image --------------------------------- //for creating a smaller size copying of uploading photo InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(); img.Resize(uploads + filename, _appEnvironment.WebRootPath + "\\upload\\thumbnailimage\\" + filename); } } //------####################------ End Uploadin Images -------######################### //Inserting if (bookId == 0) { //اگر کاربر تصویری برای کتاب مشخص نکرد نصویر پیش فرض ما ذخیره شود if (model.BookImage == null) { model.BookImage = "defaultpic.png"; } using (var db = _iServiceProvider.GetRequiredService <ApplicationDbContext>()) { //برای گفتن صریح اینکه ویو مدل ما همان کلاس بوک مااست به ام وی سی و بتواند ان را روی جدول بوک ذخیره کند //Book bookModel = _mapper.Map<AddEditBookViewModel, Book>(model); Book bookModel = Mapper.Map <AddEditBookViewModel, Book>(model); db.Books.Add(bookModel); db.SaveChanges(); } return(Json(new { status = "success", message = "کتاب با موفقیت اضافه شد" })); } //Updating else { //اگر کاربر درحالت ویرایش تصویری وارد نکرد تصویر قبلی ذخیره شده ان دوباره به ان منتصب شود if (model.BookImage == null) { model.BookImage = imgName; } using (var db = _iServiceProvider.GetRequiredService <ApplicationDbContext>()) { //برای گفتن صریح اینکه ویو مدل ما همان کلاس بوک مااست به ام وی سی و بتواند ان را روی جدول بوک ذخیره کند Book bookModel = Mapper.Map <AddEditBookViewModel, Book>(model); db.Books.Update(bookModel); db.SaveChanges(); } return(Json(new { status = "success", message = "اطلاعات کتاب با موفقیت ویرایش شد" })); } }//end model state // برای نایش کمبو باکس ها بعد خطا در ولیدیشن ها model.Authors = _context.Authors.Select(a => new SelectListItem { Text = a.AuthorName, //the value accepts the string data type as a result we should convert data type to string Value = a.AuthorId.ToString() }).ToList(); model.BookGroups = _context.BookGroups.Select(bg => new SelectListItem { Text = bg.BookGroupName, Value = bg.BookGroupId.ToString() }).ToList(); //display validation with jquery ajax var list = new List <string>(); foreach (var validation in ViewData.ModelState.Values) { list.AddRange(validation.Errors.Select(error => error.ErrorMessage)); } return(Json(new { status = "error", error = list })); // // آی دی ورودی حتما باید همنام اتریبیوت // //تگ باشد که درپارشال asp-for="BookId" // // دادیم_AddEditBookPartial // if (ModelState.IsValid ) // { // if (bookId == 0) // { // //Insert // using (var db = _iServiceProvider.GetRequiredService<ApplicationDbContext>()) // { // //برای گفتن صریح اینکه ویو مدل ما همان کلاس بوک مااست به ام وی سی و بتواند ان را روی جدول بوک ذخیره کند // //Book bookModel = _mapper.Map<AddEditBookViewModel, Book>(model); // Book bookModel = Mapper.Map<AddEditBookViewModel, Book>(model); // db.Books.Add(bookModel); // db.SaveChanges(); // } // } // else // { // //Update // using (var db = _iServiceProvider.GetRequiredService<ApplicationDbContext>()) // { // //برای گفتن صریح اینکه ویو مدل ما همان کلاس بوک مااست به ام وی سی و بتواند ان را روی جدول بوک ذخیره کند // Book bookModel = Mapper.Map<AddEditBookViewModel, Book>(model); // db.Books.Update(bookModel); // db.SaveChanges(); // } // } // return PartialView("_SuccessfullyResponsePartial", redirectUrl); // } // //برای نایش کمبو باکس ها بعد خطا در ولیدیشن ها // model.Authors = _context.Authors.Select(a => new SelectListItem // { // Text = a.AuthorName, // //the value accepts the string data type as a result we should convert data type to string // Value = a.AuthorId.ToString() // }).ToList(); // model.BookGroups = _context.BookGroups.Select(bg => new SelectListItem // { // Text = bg.BookGroupName, // Value = bg.BookGroupId.ToString() // }).ToList(); // return PartialView("_AddEditBookPartial", model); }
public async Task <IActionResult> AddEditBook(int BookId, AddEditBookViewModel model, IEnumerable <IFormFile> files, string ImgName) { if (ModelState.IsValid) { #region Begin: upload image var uploads = Path.Combine(_appEnvironment.WebRootPath, "upload\\normalimage\\"); var uploadsThubnail = Path.Combine(_appEnvironment.WebRootPath, "upload\\thumbnailimage\\"); foreach (var file in files) { if (file != null && file.Length > 0) { var filename = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); using (var fs = new FileStream(Path.Combine(uploads, filename), FileMode.Create)) { await file.CopyToAsync(fs); model.Image = filename; } // resize image InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(); img.Resize(uploads + filename, uploadsThubnail + filename); } } #endregion // insert if (BookId == 0) { _ibs.InsertBook(model); _ibs.Save(); return(Json(new { status = "success", message = "کتاب با موفقیت ثبت شد" })); } // update else { if (model.Image == null) { model.Image = ImgName; } _ibs.EditBook(model); _ibs.Save(); return(Json(new { status = "success", message = "کتاب با موفقیت بروز رسانی شد" })); } } #region Display validation with jquery ajax var list = new List <string>(); foreach (var validation in ViewData.ModelState.Values) { list.AddRange(validation.Errors.Select(error => error.ErrorMessage)); } #endregion #region reload select model.BookGroups = _ibs.GetAllBookGroup().Select(bg => new SelectListItem { Text = bg.GropuName, Value = bg.BookGroupId.ToString() }).ToList(); model.Authors = _ibs.GetAllAuthor().Select(a => new SelectListItem { Text = a.AuthorName, Value = a.AuthorId.ToString() }).ToList(); #endregion return(Json(new { status = "error", error = list })); }
//[ValidateAntiForgeryToken] public async Task <IActionResult> Add(int ProductId, ProductAddEditModel model, IEnumerable <IFormFile> files, string ImgName) { if (ModelState.IsValid) { //Upload Image var uploads = Path.Combine(_appEnvironment.WebRootPath, "upload\\normalimage\\"); foreach (var file in files) { if (file != null && file.Length > 0) { var filename = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); using (var fs = new FileStream(Path.Combine(uploads, filename), FileMode.Create)) { await file.CopyToAsync(fs); model.ProductImage = filename; } //Resize Images InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(); img.Resize(uploads + filename, _appEnvironment.WebRootPath + "\\upload\\thumbnailimage\\" + filename); } } /***********************/ if (ProductId == 0) { if (model.ProductImage == null) { model.ProductImage = "defaultpic1.png"; } //Insert using (var db = _iServiceProvider.GetRequiredService <ApplicationDbContext>()) { Product productModel = AutoMapper.Mapper.Map <ProductAddEditModel, Product>(model); db.products.Add(productModel); db.SaveChanges(); } return(Json(new { status = "success", message = "کالا با موفقیت ثبت شد" })); } else { if (model.ProductImage == null) { model.ProductImage = ImgName; } //Update using (var db = _iServiceProvider.GetRequiredService <ApplicationDbContext>()) { Product productModel = AutoMapper.Mapper.Map <ProductAddEditModel, Product>(model); db.products.Update(productModel); db.SaveChanges(); } return(Json(new { status = "success", message = "کالا با موفقیت ویراش شد" })); } } //Refreshing Category Dropdown model.Categories = _context.categories.Select(c => new SelectListItem { Text = c.Name, Value = c.CategoryId.ToString() }).ToList(); //Display Validation with Jquery Ajax var list = new List <string>(); foreach (var validation in ViewData.ModelState.Values) { list.AddRange(validation.Errors.Select(error => error.ErrorMessage)); } return(Json(new { status = "error", error = list })); }
public async Task <IActionResult> Create(News news, IFormFile newsimage, List <IFormFile> newsgallery, string tags) { if (ModelState.IsValid) { news.UserId = 1010; //news.UserId = (await _userService.GetUserByIdentity(User.Identity.Name)).UserId; var fileName = "no-photo.jpg"; if (ModelState.IsValid) { //--------------Create News Images ------------------ if (newsimage != null) { var uploads = Path.Combine(_env.WebRootPath, "Upload\\NewsImages"); fileName = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(newsimage.FileName); using (var fileStream = new FileStream(Path.Combine(uploads, fileName), FileMode.Create)) { await newsimage.CopyToAsync(fileStream); } //---------------------resize Images ---------------------- InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(128); img.Resize(Path.Combine(uploads, fileName), Path.Combine(uploads, "thumbnail", fileName)); } news.NewsImage = fileName; //------------Create Gallery Product -------------- foreach (var file in newsgallery) { var uploads = Path.Combine(_env.WebRootPath, "Upload\\NewsImages"); string galleryname = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); using (var fileStream = new FileStream(Path.Combine(uploads, galleryname), FileMode.Create)) { await file.CopyToAsync(fileStream); } news.NewsGallery.Add(new NewsGallery() { NewsId = news.NewsId, ImageName = galleryname }); //---------------------resize Images ---------------------- InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(350); img.Resize(Path.Combine(uploads, galleryname), Path.Combine(uploads, "thumbnail", galleryname)); } //-------------------Tags--------------------- if (!string.IsNullOrEmpty(tags)) { string[] tag = tags.Split('-'); foreach (string t in tag) { news.NewsTag.Add(new NewsTag() { NewsId = news.NewsId, TagsTitle = t.Trim() }); } } await _newsService.Add(news); return(RedirectToAction(nameof(Index))); } } ViewData["NewsGroupId"] = new SelectList(await _newsGroupService.GetAll(), "NewsGroupId", "AliasName", news.NewsGroupId); return(View(news)); }
public async Task <IActionResult> Edit(int id, News news, IFormFile newsimage, List <IFormFile> newsgallery, string tags) { if (id != news.NewsId) { return(NotFound()); } if (ModelState.IsValid) { try { var uploads = Path.Combine(_env.WebRootPath, "Upload\\NewsImages"); news.UserId = 1010; //news.UserId = (await _userService.GetUserByIdentity(User.Identity.Name)).UserId; //----------------------Edit News Image ----------------------- if (newsimage != null) { if (news.NewsImage != "no-photo.jpg") { if (System.IO.File.Exists(Path.Combine(uploads, news.NewsImage))) { System.IO.File.Delete(Path.Combine(uploads, news.NewsImage)); } if (System.IO.File.Exists(Path.Combine(uploads, "thumbnail", news.NewsImage))) { System.IO.File.Delete(Path.Combine(uploads, "thumbnail", news.NewsImage)); } } news.NewsImage = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(newsimage.FileName); using (var fileStream = new FileStream(Path.Combine(uploads, news.NewsImage), FileMode.Create)) { await newsimage.CopyToAsync(fileStream); } //---------------------resize Images ---------------------- InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(128); img.Resize(Path.Combine(uploads, news.NewsImage), Path.Combine(uploads, "thumbnail", news.NewsImage)); } //------------Edit Gallery Product -------------- List <NewsGallery> newsGalleries = new List <NewsGallery>(); foreach (var file in newsgallery) { string galleryname = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); using (var fileStream = new FileStream(Path.Combine(uploads, galleryname), FileMode.Create)) { await file.CopyToAsync(fileStream); } newsGalleries.Add(new NewsGallery() { NewsId = news.NewsId, ImageName = galleryname }); //---------------------resize Images ---------------------- InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(350); img.Resize(Path.Combine(uploads, galleryname), Path.Combine(uploads, "thumbnail", galleryname)); } await _newsGalleryService.Add(newsGalleries); //------------Edit TAgs ------------------ await _newsTagService.RemoveByNewsId(news.NewsId); if (!string.IsNullOrEmpty(tags)) { List <NewsTag> newsTags = new List <NewsTag>(); foreach (string tag in tags.Split('-')) { newsTags.Add(new NewsTag() { NewsId = news.NewsId, TagsTitle = tag.Trim() }); } await _newsTagService.Add(newsTags); } await _newsService.Edit(news); } catch (DbUpdateConcurrencyException) { if (!await _newsService.NewsExistence(news.NewsId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(news)); }
public ActionResult Edit(Products products, HttpPostedFileBase ProductImage, HttpPostedFileBase[] ProductGallery, string tags) { if (ModelState.IsValid) { if (ProductImage != null) { if (products.ProductImage != "no-photo.jpg") { if (System.IO.File.Exists(Server.MapPath("/ProductImages/Image/" + products.ProductImage))) { System.IO.File.Delete(Server.MapPath("/ProductImages/Image/" + products.ProductImage)); } if (System.IO.File.Exists(Server.MapPath("/ProductImages/Thumb/" + products.ProductImage))) { System.IO.File.Delete(Server.MapPath("/ProductImages/Thumb/" + products.ProductImage)); } } products.ProductImage = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(ProductImage.FileName); ProductImage.SaveAs(Server.MapPath("/ProductImages/Image/" + products.ProductImage)); InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(); img.Resize(Server.MapPath("/ProductImages/Image/" + products.ProductImage), Server.MapPath("/ProductImages/Thumb/" + products.ProductImage)); } if (ProductGallery != null && ProductGallery.Any()) { foreach (HttpPostedFileBase file in ProductGallery) { string galleryname = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); file.SaveAs(Server.MapPath("/ProductImages/Image/" + galleryname)); //--------------Resize Image For Gallery--------------------- InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(); img.Resize(Server.MapPath("/ProductImages/Image/" + galleryname), Server.MapPath("/ProductImages/Thumb/" + galleryname)); db.Product_Galleries.Add(new Product_Galleries() { ImageName = galleryname, ProductID = products.ProductID, }); } } db.Product_Tags.Where(t => t.ProductID == products.ProductID).ToList().ForEach(t => db.Product_Tags.Remove(t)); //---------------------Tags------------------------------- if (!string.IsNullOrEmpty(tags)) { string[] tag = tags.Split('-'); foreach (string t in tag) { db.Product_Tags.Add(new Product_Tags() { ProductID = products.ProductID, TagTitle = t.Trim() }); } } db.Entry(products).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.GroupID = new SelectList(db.Product_Groups, "GroupID", "GroupTitle", products.GroupID); return(View(products)); }
public async Task <IActionResult> AddEditProduct(AddEditProductViewModel model, string ImgName, int id, IEnumerable <IFormFile> files) { if (ModelState.IsValid) { //upload audio var uploads = Path.Combine(_appEnvironment.WebRootPath, "upload\\normalimage\\"); foreach (var file in files) { if (file != null && file.Length > 0) { var filename = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); using (var fs = new FileStream(Path.Combine(uploads, filename), FileMode.Create)) { await file.CopyToAsync(fs); model.ImageName = filename; } InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(); img.Resize(uploads + filename, _appEnvironment.WebRootPath + "\\upload\\thumbnailimage\\" + filename); } } //upload audio if (id == 0) { if (model.ImageName == null) { model.ImageName = "defaultpic.png"; } using (var db = _serviceProvider.GetRequiredService <ApplicationDbContext>()) { Product productModel = AutoMapper.Mapper.Map <AddEditProductViewModel, Product>(model); db.Products.Add(productModel); await db.SaveChangesAsync(); } TempData["Notif"] = Notification.ShowNotif(MessageType.Add, type: ToastType.green); //return PartialView("_Succefullyresponse", redirecturl); //return Json(new { status = "success", message = "محصول با موفقیت ایجاد شد" }); return(RedirectToAction("Index")); } else { if (model.ImageName == null) { model.ImageName = ImgName; } using (var db = _serviceProvider.GetRequiredService <ApplicationDbContext>()) { Product productModel = AutoMapper.Mapper.Map <AddEditProductViewModel, Product>(model); db.Products.Update(productModel); await db.SaveChangesAsync(); } TempData["Notif"] = Notification.ShowNotif(MessageType.Edit, type: ToastType.blue); //return Json(new { status = "success", message = "اطلاعات محصول با موفقیت ویرایش شد" }); //return PartialView("_Succefullyresponse", redirecturl); return(RedirectToAction("Index")); } } if (id == 0) { TempData["Notif"] = Notification.ShowNotif(MessageType.addError, type: ToastType.yellow); } else { TempData["Notif"] = Notification.ShowNotif(MessageType.editError, type: ToastType.yellow); } model.CategoryList = await _context.Categories.Select(c => new SelectListItem() { Text = c.Title, Value = c.Id.ToString() }).ToListAsync(); model.UnitList = await _context.Units.Select(u => new SelectListItem() { Text = u.Title, Value = u.Id.ToString() }).ToListAsync(); return(PartialView("AddEditProduct", model)); }
public async Task <IActionResult> AddEditProductGallery(AddEditProductGallery model, int id, IEnumerable <IFormFile> files) { if (ModelState.IsValid) { List <string> stList = new List <string>(); var upload = Path.Combine(_appEnvironmen.WebRootPath, "upload\\normalimage\\"); foreach (var faile in files) { if (faile != null && faile.Length > 0) { var filename = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(faile.FileName); using (var fs = new FileStream(Path.Combine(upload, filename), FileMode.Create)) { await faile.CopyToAsync(fs); stList.Add(filename); //model.Image = filename; } InsertShowImage.ImageResizer img = new InsertShowImage.ImageResizer(); img.Resize(upload + filename, _appEnvironmen.WebRootPath + "\\upload\\thumbnailimage\\" + filename); } } if (model.Image == null) { model.Image = "defaultpic.png"; } if (id == 0) { using (var db = _ServiceProvider.GetRequiredService <ApplicationDbContext>()) { foreach (var VARIABLE in stList) { model.Image = VARIABLE; model.IdProduct = (int)HttpContext.Session.GetInt32("ImageProductKey"); ProductGallery imagemodel = AutoMapper.Mapper.Map <AddEditProductGallery, ProductGallery>(model); db.ProductGalleries.Add(imagemodel); db.SaveChanges(); } } return(Json(new { Status = "success", Message = "عکس با موفقیت ثبت شد" })); } else { using (var db = _ServiceProvider.GetRequiredService <ApplicationDbContext>()) { foreach (var VARIABLE in stList) { model.Image = VARIABLE; model.IdProduct = (int)HttpContext.Session.GetInt32("ImageProductKey"); ProductGallery imagemodel = AutoMapper.Mapper.Map <AddEditProductGallery, ProductGallery>(model); db.ProductGalleries.Update(imagemodel); db.SaveChanges(); } } return(Json(new { Status = "success", Message = "عکس با موفقیت ویرایش شد" })); } } model.ProductListItems = _Context.Products.Select(a => new SelectListItem { Text = a.Name, Value = a.Id.ToString() }).ToList(); var list = new List <string>(); foreach (var validation in ViewData.ModelState.Values) { list.AddRange(validation.Errors.Select(error => error.ErrorMessage)); } return(Json(new { Status = "error", error = list })); }