public ActionResult AddPhoto(int id, HttpPostedFileBase file) { if (id > 0 && file != null) { string extension = file.FileName.Substring(file.FileName.LastIndexOf('.') + 1).ToLower(); string fileName = string.Format("{0}.{1}", Guid.NewGuid(), extension); string fullPath = string.Format(@"{0}\content\images\photos\orig\{1}", Request.PhysicalApplicationPath, fileName); string origPath = string.Format("/content/images/photos/orig/{0}", fileName); string resizePath = string.Format("/content/images/photos/{0}", fileName); string thumbPath = string.Format("/content/images/photos/thumbs/{0}", fileName); string mimeType = file.ContentType; file.SaveAs(fullPath); ImageUtils.CreateZoomedThumbnail(origPath, thumbPath, 150); ImageUtils.ResizeImage(origPath, resizePath, 720, false, ""); var album = db.GetAlbumByID(id); var photo = new Photo(); photo.FileName = fileName; if (album.Photos.Count > 0) photo.SortOrder = album.Photos.Max(x => x.SortOrder) + 1; album.AddPhoto(photo); db.SaveAlbum(album); TempData["message"] = string.Format("New photo added to album with filename = {0}", fileName); return RedirectToAction("Edit", new { id = id }); } return View(); }
public void AddPhoto(Photo photo) { photo.Album = this; Photos.Add(photo); }
public string AddPhotoAjax(int id, HttpPostedFileBase file) { if (id > 0 && file != null) { string extension = file.FileName.Substring(file.FileName.LastIndexOf('.') + 1).ToLower(); string fileName = string.Format("{0}.{1}", Guid.NewGuid(), extension); string fullPath = string.Format(@"{0}\content\images\photos\orig\{1}", Request.PhysicalApplicationPath, fileName); string origPath = string.Format("/content/images/photos/orig/{0}", fileName); string resizePath = string.Format("/content/images/photos/{0}", fileName); string thumbPath = string.Format("/content/images/photos/thumbs/{0}", fileName); string mimeType = file.ContentType; file.SaveAs(fullPath); ImageUtils.CreateZoomedThumbnail(origPath, thumbPath, 150); ImageUtils.ResizeImage(origPath, resizePath, 720, false, ""); var album = db.GetAlbumByID(id); var photo = new Photo(); photo.FileName = fileName; if (album.Photos.Count > 0) photo.SortOrder = album.Photos.Max(x => x.SortOrder) + 1; album.AddPhoto(photo); db.SaveAlbum(album); System.Threading.Thread.Sleep(1000); return "Photo saved"; } return "No file found"; }
public void SavePhoto(Photo photo) { context.Entry(photo).State = photo.ID == 0 ? EntityState.Added : EntityState.Modified; context.SaveChanges(); }