public ActionResult DeleteImage(long?id) { try { long realEstateID = 0; if (id.HasValue) { RealEStateImage image = db.RealEStateImages.Find(id.Value); realEstateID = image.RealEStateID; db.RealEStateImages.Remove(image); db.SaveChanges(); } else { TempData["Message"] = "Please select an image to delete!"; } return(RedirectToAction("ListImages", new { id = realEstateID })); } catch (Exception ex) { ViewBag.ErrorMessage = ex.Message; return(View("Error")); } }
public ActionResult UploadImage(long?id) { try { var allowedExtensions = new[] { ".jpg", ".png", ".jpeg", "bmp" }; HttpPostedFileBase file = Request.Files["realEsateImage"]; if (file.ContentLength > (2048 * 1000)) { TempData["Message"] = "Maximum allowed size is 2MB."; return(RedirectToAction("ListImages", new { id = id })); } var extension = Path.GetExtension(file.FileName); if (!allowedExtensions.Contains(extension)) { TempData["Message"] = "Selected file not allowed. Allowed types are: jpg, png, jpeg and bmp."; } else { int fileLength = file.ContentLength; byte[] fileData = new byte[fileLength]; file.InputStream.Read(fileData, 0, fileLength); RealEStateImage image = new RealEStateImage() { ImageTitle = "Image", RealEStateID = id.Value, ImageDate = DateTime.Now, ImageData = fileData }; db.RealEStateImages.Add(image); db.SaveChanges(); } return(RedirectToAction("ListImages", new { id = id })); } catch (Exception ex) { ViewBag.ErrorMessage = ex.ToString(); return(View("Error")); } }