public Photo SavePhotoToFileSystem(Photo photo, HttpPostedFileBase photoUpload) { var uploadDirectory = "/Photos/" + photo.GalleryId + "/"; var imagePath = uploadDirectory + photoUpload.FileName; photoUpload.SaveAs(imagePath); photo.FilePath = imagePath; photo.ThumbnailPath = GenerateThumbnail(photo.FilePath); return photo; }
public PhotoViewModel(Photo photo, string galleryName) { Title = photo.Title; Description = photo.Description; PhotoPath = photo.FilePath; GalleryName = galleryName; GalleryId = photo.GalleryId; PhotoId = photo.PhotoId; Height = photo.Height; Width = photo.Width; }
public List<Photo> UploadMultiplePhotos(IEnumerable<HttpPostedFileBase>Uploads, int galleryId) { List<Photo> PhotosAdded = new List<Photo>(); foreach (var upload in Uploads) { Photo photo = new Photo() { Title = Path.GetFileNameWithoutExtension(upload.FileName), GalleryId = galleryId, Height = Image.FromStream(upload.InputStream, false, false).Height, Width = Image.FromStream(upload.InputStream, false, false).Width }; photo = SavePhotoToFileSystem(photo, upload); PhotosAdded.Add(photo); } return PhotosAdded; }
public ActionResult Create(CreatePhotoViewModel model) { if (model.PhotoUpload == null || model.PhotoUpload.ContentLength == 0) { ModelState.AddModelError("PhotoUpload", "This field is required"); } if (model.PhotoUpload.ContentLength >= 5000000) { ModelState.AddModelError("PhotoUpload", "Photo is too big! Please resize to under 5mb or contact Alex"); } if (ModelState.IsValid) { PhotoUploader photoUploader = new PhotoUploader(); var photo = new Photo { Title = model.Title, Description = model.Description, GalleryId = model.GalleryId, FileName = model.PhotoUpload.FileName, Width = Image.FromStream(model.PhotoUpload.InputStream, false, false).Width, Height = Image.FromStream(model.PhotoUpload.InputStream, false, false).Height }; if (model.PhotoUpload != null && model.PhotoUpload.ContentLength > 0) { photo = photoUploader.SavePhotoToFileSystem(photo, model.PhotoUpload); repository.Add(photo); repository.Save(); } return RedirectToAction("ViewGallery", "Gallery", new {id = model.GalleryId}); } return View(model); }