예제 #1
0
        public ActionResult Create(Photo photo, IEnumerable<HttpPostedFileBase> files)
        {
            var context = new TourEntities1();
            if (!ModelState.IsValid)
                return View(photo);
            if (files.Count() == 0 || files.FirstOrDefault() == null)
            {
                ViewBag.error = "Please choose a file";
                return View(photo);
            }

            foreach (var file in files)
            {
                if (file.ContentLength == 0) continue;
                var model = new Photo();
                model.Decription = photo.Decription;
                var fileName = Guid.NewGuid().ToString();
                var extension = System.IO.Path.GetExtension(file.FileName).ToLower();

                MemoryStream ms = new MemoryStream();
                file.InputStream.CopyTo(ms);
                file.InputStream.Dispose();
                ms.Seek(0, SeekOrigin.Begin);
                //var img = System.Drawing.Image.FromStream(ms);
                //img.Save("./asd123.png");

                using (var img1 = System.Drawing.Image.FromStream(ms))
                {
                    //model.ThumbPath = String.Format("~{2}ImageGallery{2}thumbs{2}{0}{1}", fileName, extension, Path.DirectorySeparatorChar);
                    //model.ImagePath = String.Format("~{2}ImageGallery{2}{0}{1}", fileName, extension, Path.DirectorySeparatorChar);
                    model.ThumbPath = String.Format("/ImageGallery/thumbs/{0}{1}", fileName, extension);
                    model.ImagePath = String.Format("/ImageGallery/{0}{1}", fileName, extension);
                    // Save thumbnail size image, 100 x 100
                    SaveToFolder(img1, fileName, extension, new Size(100, 100), model.ThumbPath);

                    // Save large size image, 800 x 800
                    SaveToFolder(img1, fileName, extension, new Size(600, 600), model.ImagePath);
                }

                // Save record to database
                model.CreatedOn = DateTime.Now;
                context.Photo.Add(model);
                context.SaveChanges();
            }

            return RedirectToAction("Index", "Photo");
        }
예제 #2
0
 public ActionResult Create()
 {
     var photo = new Photo();
     return View(photo);
 }