public async Task<ActionResult> Upload(LocationImageViewModel viewModel) { System.Diagnostics.Debug.WriteLine("----------------------------------------"); System.Diagnostics.Debug.WriteLine("LocationImageController.Upload()"); // List of allowed image types (for hosting on web) var validImageTypes = new string[] { "image/gif", "image/jpeg", "image/pjpeg", // needed for compatability with some older jpegs "image/png" }; // 1. check if file is null, or of zero length if (viewModel.ImageUpload == null || viewModel.ImageUpload.ContentLength == 0) { System.Diagnostics.Trace.WriteLine("Image is null"); ModelState.AddModelError("ImageUpload", "This field is required."); } // 2. check if file is null, or of zero length else if (viewModel.ImageUpload == null || viewModel.ImageUpload.ContentLength == 0) { System.Diagnostics.Trace.WriteLine("Uploaded file is empty"); ModelState.AddModelError("ImageUpload", "Image file cannot be empty."); } // 3. check that image is of a valid filetype else if (!validImageTypes.Contains(viewModel.ImageUpload.ContentType)) { System.Diagnostics.Trace.WriteLine("Invalid Image Type"); ModelState.AddModelError("ImageUpload", "Please choose either a GIF, JPG or PNG image."); } if (ModelState.IsValid) { System.Diagnostics.Debug.WriteLine("ModelState is valid!"); // Upload Image to Blob Storage string imageUrl = await photoService.UploadPhotoAsync(viewModel.ImageUpload); System.Diagnostics.Debug.WriteLine("Uploaded image to URL: {1}", imageUrl); // create var locationImage = new LocationImage { LocationID = viewModel.LocationID, Title = viewModel.Title, AltText = "Image from " + db.Locations.Find(viewModel.LocationID).Label, DateTaken = viewModel.DateTaken, DateCreated = DateTime.UtcNow, DateModified = DateTime.UtcNow, ImageUrl = imageUrl, User = manager.FindById(User.Identity.GetUserId()) }; // save LocationImage model in database db.LocationImages.Add(locationImage); db.SaveChanges(); System.Diagnostics.Debug.WriteLine("Saved {0} '{1}' to the database.", locationImage.AltText, locationImage.Title); return RedirectToAction("Media", "Location", new { locationID = viewModel.LocationID }); } ViewBag.PlaceholderUrl = "~/Content/ImagePreview.png"; ViewBag.LocationLabel = db.Locations.Find(viewModel.LocationID).Label; return View(viewModel); }
public ActionResult CreateByUrl(LocationImageUrlViewModel model) { // TODO finish // List of allowed image types (for hosting on web) var validImageTypes = new string[] { ".gif", ".jpg", ".jpeg", ".pjpeg", // needed for compatability with some older jpegs ".png" }; // 1. check if a valid image was uploaded if (model.ImageUrl == null || model.ImageUrl.Length == 0) { System.Diagnostics.Debug.WriteLine("Image is null or empty"); ModelState.AddModelError("ImageUpload", "This field is required"); } // 2. check that image is of a valid filetype else { bool hasValidExtension = false; foreach (String s in validImageTypes) { if (model.ImageUrl.EndsWith(s)) { hasValidExtension = true; break; } } if (!hasValidExtension) { System.Diagnostics.Debug.WriteLine("Invalid Image Type"); ModelState.AddModelError("ImageUpload", "Please choose either a GIF, JPG or PNG image."); } } // 3. create the Image entry to save to the database if (ModelState.IsValid) { System.Diagnostics.Debug.WriteLine("Model state is valid"); // initialize image model to store in database var locationImage = new LocationImage { LocationID = model.LocationID, Title = model.Title, AltText = "Image from " + db.Locations.Find(model.LocationID).Label, DateTaken = model.DateTaken, DateCreated = DateTime.UtcNow, DateModified = DateTime.UtcNow, ImageUrl = model.ImageUrl, User = manager.FindById(User.Identity.GetUserId()) }; // save LocationImage model in database db.LocationImages.Add(locationImage); db.SaveChanges(); return RedirectToAction("Media", "Location", new { locationID = model.LocationID }); } // otherwise, go back to create view. System.Diagnostics.Debug.WriteLine("Model state is invalid."); ViewBag.LocationID = model.LocationID; ViewBag.LocationLabel = db.Locations.Find(model.LocationID).Label; ViewBag.PlaceholderUrl = "~/Content/ImagePreview.png"; ViewBag.LocationCount = db.Locations.Count(); return View(model); }