public ActionResult ChangeProfilePicture(int? id, HttpPostedFileBase file) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Renter renter = db.Renters.Find(id); if (renter == null) { return HttpNotFound(); } if (file != null) { if (file.ContentType != "image/jpeg" && file.ContentType != "image/png" && file.ContentType != "image/gif") { return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "File format not supported. Please try a different file."); } try { ImageService imageService = new ImageService(); var imageId = imageService.AddImage(file); if (imageId == 0) { return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "There was a problem uploading the image."); } else { RenterService.ChangeProfilePicture(id, imageId); return RedirectToAction("Edit", new { id = renter.Id }); } } catch (ArgumentException ex) { return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "There was a problem uploading the image or generating thumbnails. File format may be invalid. Please try a different image."); } catch (OutOfMemoryException ex) { return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "There was a problem uploading the image or generating thumbnails. Image may be corrupt. Please try a different image."); } } else { return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Please select an image to upload."); } }
public ImageController() { ImageService = new ImageService(); }