Ejemplo n.º 1
0
        public ActionResult Create(Photo photo, IEnumerable<HttpPostedFileBase> files)
        {
            if (!ModelState.IsValid)
            {
                return View(photo);
            }
            if (files.Count() == 0 || files.FirstOrDefault() == null)
            {
                ViewBag.error = "Please choose a file";
                return View(photo);
            }
            var model = new Photo();
            foreach (var file in files)
            {
                if (file.ContentLength == 0) continue;
                model.Description = photo.Description;
                var fileName = Guid.NewGuid().ToString();
                var s = Path.GetExtension((file.FileName));
                if (s != null)
                {
                    var extension = s.ToLower();
                    using (var img = Image.FromStream(file.InputStream))
                    {
                        model.ThumbPath = string.Format("/GalleryImages/thumbs/{0}{1}", fileName, extension);
                        model.ImagePath = string.Format("/GalleryImages/{0}{1}", fileName, extension);
                        SaveToFolder(img, fileName, extension, new Size(100, 100), model.ThumbPath);
                        SaveToFolder(img, fileName, extension, new Size(600, 600), model.ImagePath);

                        model.CreateOn = DateTime.Now;

                        _bd.Photos.Add(model);
                        _bd.SaveChanges();
                    }
                }
            }

            return RedirectPermanent("index");
        }
Ejemplo n.º 2
0
 public ActionResult Create()
 {
     var photo = new Photo();
     return View(photo);
 }