public ActionResult Create(ImageSlide model)
        {
            if (ModelState.IsValid)
            {
                // Verify that the user selected a file
                if (model.ImageFile != null && model.ImageFile.ContentLength > 0)
                {
                    var fileType = Path.GetExtension(model.ImageFile.FileName);
                    var fileName = Guid.NewGuid().ToString() + fileType;

                    // store the file inside ~/Images/Uploads folder
                    var path = Path.Combine(Server.MapPath("~/Images/Uploads"), fileName);
                    model.ImageFile.SaveAs(path);

                    // Save web url
                    model.ImageUrl = Url.Content(string.Format("~/Images/Uploads/{0}", fileName));
                }

                db.ImageSlides.Add(model);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(model));
        }
예제 #2
0
 public ActionResult AddImage(HttpPostedFileBase ImagePath)
 {
     if (ImagePath != null)
     {
         Image img = Image.FromStream(ImagePath.InputStream);
         if ((img.Width != 800) || (img.Height != 356))
         {
             ModelState.AddModelError("", "Image resolution must be 800 x 356 pixels");
             return(View());
         }
         string pic  = Path.GetFileName(ImagePath.FileName);
         string path = Path.Combine(Server.MapPath("~/Content/images/"), pic);
         ImagePath.SaveAs(path);
         using (SliderContext db = new SliderContext())
         {
             Gallery gallery = new Gallery
             {
                 ImagePath = "~/Content/images/" + pic
             };
             db.gallery.Add(gallery);
             db.SaveChanges();
         }
     }
     return(RedirectToAction("Index"));
 }
예제 #3
0
 public ActionResult DeleteImages(IEnumerable <int> ImagesIds)
 {
     using (SliderContext db = new SliderContext())
     {
         foreach (var id in ImagesIds)
         {
             var    image   = db.gallery.Single(s => s.GalleryID == id);
             string imgPath = Server.MapPath(image.ImagePath);
             db.gallery.Remove(image);
             if (System.IO.File.Exists(imgPath))
             {
                 System.IO.File.Delete(imgPath);
             }
         }
         db.SaveChanges();
     }
     return(RedirectToAction("DeleteImages"));
 }