public ActionResult Edit([Bind(Include = "ActorId,ActorFirstName,ActorLastName,Address,City,State,ZipCode,PhoneNumber,AgencyID,ActorPhoto,SpecialNotes,DateAdded,IsActive")] Actor actor, HttpPostedFileBase actorheadshot) { if (ModelState.IsValid) { #region Image Upload if (actorheadshot != null) { /**/ string imgName = actorheadshot.FileName; /*1*/ string ext = imgName.Substring(imgName.LastIndexOf('.')); /*2*/ string[] goodExts = { ".jpeg", ".jpg", ".gif", ".png" }; /*3*/ if (goodExts.Contains(ext.ToLower()) && (actorheadshot.ContentLength <= 4193404)) { /*4*/ imgName = Guid.NewGuid() + ext.ToLower(); //commented this out and followed this other project file //actorheadshot.SaveAs(Server.MapPath("~/Content/actorheadshots/" + imgName)); #region Resize Image /*5*/ string savePath = Server.MapPath("~/Content/actorheadshots/"); //taking the contents of this file and creating a stream of bytes, http file base type is becoming a stream of bytes into a type of image. this conversion has to take place for us to be able to resize the image /*6*/ Image convertedImage = Image.FromStream(actorheadshot.InputStream); int maxImageSize = 500; int maxThumbSize = 100; //if you allowed image uploads for magazine and books - you would need to repeat that code - that's why the image service code is in an imageservice area /*7*/ UploadUtility.ResizeImage(savePath, imgName, convertedImage, maxImageSize, maxThumbSize); UploadUtility.Delete(savePath, actor.ActorPhoto); actor.ActorPhoto = imgName; //saves image onto server - but doesn't update db need to make sure to update what is stored in the db #endregion } else { imgName = "nouserimg.png"; } actor.ActorPhoto = imgName; } #endregion db.Entry(actor).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.AgencyID = new SelectList(db.UserDetails, "UserID", "FirstName", actor.AgencyID); return(View(actor)); }
public ActionResult Edit([Bind(Include = "LessonID,LessonTitle,CourseID,Introduction,VideoURL,PdfFilename,IsActive")] Lesson lesson, HttpPostedFileBase lessonFile) { if (ModelState.IsValid) { #region File Upload if (lessonFile != null) { string file = lessonFile.FileName; string[] goodExts = new string[] { ".jpg", ".jpeg", ".png", ".gif", ".pdf" }; //check that the uploaded file ext is in our list of good file extensions && check file size <= 4mb (max by default from ASP.NET) if (goodExts.Contains(file.Substring(file.LastIndexOf("."))) && lessonFile.ContentLength <= 4194304) { if (lesson.PdfFilename != null && lesson.PdfFilename != "JustinLKennedyResume.pdf") //Delete the old file of the record that is being edited { string path = Server.MapPath("~/Content/images/lessons/"); UploadUtility.Delete(path, lesson.PdfFilename); } lessonFile.SaveAs(Server.MapPath("~/Content/images/lessons/" + file)); } lesson.PdfFilename = file; } #endregion db.Entry(lesson).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CourseID = new SelectList(db.Courses, "CourseID", "CourseName", lesson.CourseID); return(View(lesson)); }
public ActionResult Edit([Bind(Include = "OwnerAssetId,CarName,OwnerId,CarPhoto,SpecialNotes,IsActive,DateAdded")] Car car, HttpPostedFileBase CarPhoto) { #region File Upload if (CarPhoto != null)//HttpPostedFileBase added to the action != null { //Get image and assign to variable string imgName = CarPhoto.FileName; //Declare and assign ext value string ext = imgName.Substring(imgName.LastIndexOf('.')); //Declare a list of valid extensions. string[] goodExts = { ".jpeg", ".jpg", ".gif", ".png" }; //Check the ext value (toLower()) against the valid list if (goodExts.Contains(ext.ToLower()) && (CarPhoto.ContentLength <= 4194304))//4MB max allowed by ASP.NET { //If it is in the list rename using a guid imgName = Guid.NewGuid() + ext; //Save to the webserver CarPhoto.SaveAs(Server.MapPath("~/Content/assets/img/" + imgName)); //Create variables to resize image. string savePath = Server.MapPath("~/Content/assets/img/"); Image convertedImage = Image.FromStream(CarPhoto.InputStream); int maxImgSize = 500; int maxThumbSize = 100; UploadUtility.ResizeImage(savePath, imgName, convertedImage, maxImgSize, maxThumbSize); //Make sure you are not deleting your default image. if (car.CarPhoto != null && car.CarPhoto != "GenericCar.jpg") { //Remove the original file string path = Server.MapPath("~/Content/assets/img/"); UploadUtility.Delete(path, car.CarPhoto); } //Only save if image meets criteria imageName to the object. car.CarPhoto = imgName; } } #endregion if (ModelState.IsValid) { db.Entry(car).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.OwnerId = new SelectList(db.UserDetails, "UserId", "FirstName", car.OwnerId); return(View(car)); }
public ActionResult Edit([Bind(Include = "HomeId,HomeName,Address,City,State,ZipCode,OwnderId,HomePhoto,SpecialNotes,IsActive,DateAdded")] Homes homes, HttpPostedFileBase homePhoto) { if (ModelState.IsValid) { #region File Upload if (homePhoto != null) { string file = homePhoto.FileName; //we need to make sure they are actually uploading an appropriate file type string ext = file.Substring(file.LastIndexOf('.')); string[] goodExts = { ".jpeg", ".jpg", ".png", ".gif" }; //check that the uploaded file is in our list of good file extensions if (goodExts.Contains(ext)) { //if valid ext, check file size <= 4mb (max by default from ASP.net) if (homePhoto.ContentLength <= 52428800) // specifying in bytes how big file can be { //create a new file name using a guid - a lot of users probably have images with the same names so we change it from what the user had to a guid Globally Unique Identifier file = Guid.NewGuid() + ext; #region Resize Image string savePath = Server.MapPath("~/Content/assets/img/Uploads/"); //taking the contents of this file and creatign a stream of bytes, http file base type is becmonig a stream of bytes into a type of image. this conversion has to take place for us to be able to resize the image Image convertedImage = Image.FromStream(homePhoto.InputStream); int maxImageSize = 500; int maxThumbSize = 100; //if you allowed image uploads for magazine and books - you would need to repeat that code - that's why the image service code is in an imageservice area UploadUtility.ResizeImage(savePath, file, convertedImage, maxImageSize, maxThumbSize); //saves image onto server - but doesn't update db need to make sure to update what is stored in the db #endregion if (homes.HomePhoto != null && homes.HomePhoto != "noimage.png") { string path = Server.MapPath("~/Content/assets/img/Uploads/"); UploadUtility.Delete(path, homes.HomePhoto); } } } homes.HomePhoto = file; } #endregion db.Entry(homes).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.OwnderId = new SelectList(db.UserDetails, "UserId", "CompanyName", homes.OwnderId); return(View(homes)); }
public ActionResult DeleteConfirmed(string id) { UserDetail userDetail = db.UserDetails.Find(id); if (userDetail.UserPhoto != null && userDetail.UserPhoto != "nouserimg.png") { UploadUtility.Delete(Server.MapPath("~/Content/agencylogo/"), userDetail.UserPhoto); } db.UserDetails.Remove(userDetail); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult DeleteConfirmed(int id) { Lesson lesson = db.Lessons.Find(id); //Delete the file of the record that is being removed if (lesson.PdfFilename != null) { string path = Server.MapPath("~/Content/images/lessons/"); UploadUtility.Delete(path, lesson.PdfFilename); } db.Lessons.Remove(lesson); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult DeleteConfirmed(int id) { Course course = db.Courses.Find(id); //Delete the image file of the record that is being removed if (course.CourseImage != null && course.CourseImage != "NoImage.png") { string path = Server.MapPath("~/Content/images/courses/"); UploadUtility.Delete(path, course.CourseImage); } db.Courses.Remove(course); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult DeleteConfirmed(int id) { Car car = db.Cars.Find(id); if (car.CarPhoto != null && car.CarPhoto != "GenericCar.jpg") { //Remove the original file from the edit view. string path = Server.MapPath("~/Content/assets/img/"); UploadUtility.Delete(path, car.CarPhoto); } db.Cars.Remove(car); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult DeleteConfirmed(int id) { Actor actor = db.Actors.Find(id); #region Image utility if (actor.ActorPhoto != null && actor.ActorPhoto != "nouserimg.png") { UploadUtility.Delete(Server.MapPath("~/Content/actorheadshots/"), actor.ActorPhoto); } #endregion db.Actors.Remove(actor); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult DeleteConfirmed(int id) { AuditionLocation auditionLocation = db.AuditionLocations.Find(id); #region Image utility if (auditionLocation.AuditionPhoto != null && auditionLocation.AuditionPhoto != "nouserimg.png") { UploadUtility.Delete(Server.MapPath("~/Content/auditionlocations/"), auditionLocation.AuditionPhoto); } #endregion db.AuditionLocations.Remove(auditionLocation); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "CourseID,CourseName,CourseDescription,CourseImage,IsActive")] Course course, HttpPostedFileBase courseImage) { if (ModelState.IsValid) { #region File Upload if (courseImage != null) { string file = courseImage.FileName; string ext = file.Substring(file.LastIndexOf('.')); string[] goodExts = { ".jpeg", ".jpg", ".png", ".gif" }; //check that the uploaded file ext is in our list of good file extensions if (goodExts.Contains(ext)) { //if valid ext, check file size <= 4mb (max by default from ASP.NET) if (courseImage.ContentLength <= 4194304) { //create a new file name using a guid //file = Guid.NewGuid() + ext; #region Resize Image string savePath = Server.MapPath("~/Content/images/courses/"); Image convertedImage = Image.FromStream(courseImage.InputStream); int maxImageSize = 800; int maxThumbSize = 350; UploadUtility.ResizeImage(savePath, file, convertedImage, maxImageSize, maxThumbSize); #endregion if (course.CourseImage != null && course.CourseImage != "NoImage.png") { string path = Server.MapPath("~/Content/images/courses/"); UploadUtility.Delete(path, course.CourseImage); } } } course.CourseImage = file; } #endregion db.Entry(course).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(course)); }