public override ActionResult Upload(string path, HttpPostedFileBase file) { var value = base.Upload(path, file); var physicalPath = Path.Combine(Server.MapPath(path), file.FileName); ImageOpt.ScaleImage(file, physicalPath, article_pic_width, article_pic_height); return(value); }
public ActionResult Create(HomeGallery homegallery, IEnumerable <HttpPostedFileBase> newsPhotos) { int count = 0; if (ModelState.IsValid) { // The Name of the Upload component is "newsPhotos" if ((newsPhotos == null) || (newsPhotos.Count() == 0)) { ModelState.AddModelError("", "请检查您是否忘记上传图片了"); return(View(homegallery)); } foreach (var file in newsPhotos) { if (!HasFile(file)) { ModelState.AddModelError("", "请检查您是否忘记上传图片了?"); return(View(homegallery)); } // Some browsers send file names with full path. This needs to be stripped. var fileName = Path.GetFileName(file.FileName); var physicalPath = Path.Combine(Server.MapPath(gallery_path), fileName); while (System.IO.File.Exists(physicalPath)) { fileName = count.ToString() + fileName; physicalPath = Path.Combine(Server.MapPath(gallery_path), fileName); } file.SaveAs(physicalPath); string full_fileName = full_prefix + fileName; string full_path = Path.Combine(Server.MapPath(gallery_path), full_fileName); ImageOpt.ScaleImage(physicalPath, full_path, full_width, full_height); string thumbnail_path = Path.Combine(Server.MapPath(gallery_path), thumbnail_prefix + full_fileName); ImageOpt.ScaleImage(physicalPath, thumbnail_path, thumbnail_width, thumbnail_height); System.IO.File.Delete(physicalPath); homegallery.Picture = Url.Content(gallery_path + "/" + full_fileName); } if (!String.IsNullOrEmpty(homegallery.HyperLink)) { homegallery.HyperLink = GetAbsoluteUrl(homegallery.HyperLink); } db.HomeGalleries.Add(homegallery); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(homegallery)); }
public ActionResult Edit(Paragraph paragraph, IEnumerable <HttpPostedFileBase> newsPhotos) { int count = 0; if (ModelState.IsValid) { // The Name of the Upload component is "newsPhotos" if ((newsPhotos != null) && (newsPhotos.Count() > 0)) { foreach (var file in newsPhotos) { if (!HasFile(file)) { continue; } // Some browsers send file names with full path. This needs to be stripped. var fileName = Path.GetFileName(file.FileName); var physicalPath = Path.Combine(Server.MapPath(news_pic_path), fileName); while (System.IO.File.Exists(physicalPath)) { fileName = count.ToString() + fileName; physicalPath = Path.Combine(Server.MapPath(news_pic_path), fileName); } file.SaveAs(physicalPath); string scaled_fileName = news_pic_scaled_prefix + fileName; string scaled_physicalPath = Path.Combine(Server.MapPath(news_pic_path), scaled_fileName); ImageOpt.ScaleImage(physicalPath, scaled_physicalPath, news_pic_width, news_pic_height); System.IO.File.Delete(physicalPath); if (!String.IsNullOrEmpty(paragraph.Image)) {//delete the previous one System.IO.File.Delete(Server.MapPath(paragraph.Image)); } paragraph.Image = Url.Content(news_pic_path + "/" + scaled_fileName); } } db.Entry(paragraph).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Details", "News", new { id = paragraph.NewsId })); } return(View(paragraph)); }
public ActionResult EditGalleryItem(GalleryItem galleryItem, IEnumerable <HttpPostedFileBase> newsPhotos) { int count = 0; if (ModelState.IsValid) { if ((newsPhotos != null) && (newsPhotos.Count() > 0)) { // The Name of the Upload component is "newsPhotos" foreach (var file in newsPhotos) { if (!HasFile(file)) { continue; } // Some browsers send file names with full path. This needs to be stripped. var fileName = Path.GetFileName(file.FileName); var physicalPath = Path.Combine(Server.MapPath(gallery_path), fileName); while (System.IO.File.Exists(physicalPath)) { fileName = count.ToString() + fileName; physicalPath = Path.Combine(Server.MapPath(gallery_path), fileName); } file.SaveAs(physicalPath); string full_fileName = prefix + fileName; string full_path = Path.Combine(Server.MapPath(gallery_path), full_fileName); ImageOpt.ScaleImage(physicalPath, full_path, full_width, full_height); System.IO.File.Delete(physicalPath); System.IO.File.Delete(Server.MapPath(galleryItem.Picture)); galleryItem.Picture = Url.Content(gallery_path + "/" + full_fileName); } } if (!String.IsNullOrEmpty(galleryItem.HyperLink)) { galleryItem.HyperLink = GetAbsoluteUrl(galleryItem.HyperLink); } db.Entry(galleryItem).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(galleryItem)); }