コード例 #1
0
        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);
        }
コード例 #2
0
 public ActionResult Upload(int locationID)
 {         
     ViewBag.PlaceholderUrl = "~/Content/ImagePreview.png";
     ViewBag.LocationLabel = db.Locations.Find(locationID).Label;
     LocationImageViewModel viewModel = new LocationImageViewModel();
     viewModel.LocationID = locationID;
     return View(viewModel);
 }