private ImageResult UploadFile(HttpPostedFileBase file, string fileName) { ImageResult imageResult = new ImageResult { Success = true, ErrorMessage = null }; var path = Path.Combine(HttpContext.Current.Request.MapPath(config.PhotoUploadPath), fileName); string extension = Path.GetExtension(file.FileName); //make sure the file is valid if (!ValidateExtension(extension)) { imageResult.Success = false; imageResult.ErrorMessage = "Invalid Extension"; return(imageResult); } try { file.SaveAs(path); Image imgOriginal = Image.FromFile(path); //pass in whatever value you want Image imgActual = Scale(imgOriginal); imgOriginal.Dispose(); imgActual.Save(path); imgActual.Dispose(); imageResult.ImageName = fileName; return(imageResult); } catch (Exception ex) { // you might NOT want to show the exception error for the user // this is generally logging or testing imageResult.Success = false; imageResult.ErrorMessage = ex.Message; return(imageResult); } }
private string ProcessAndUploadPicture(Member Member, HttpPostedFileBase file) { if (file != null) { string _fileName = Path.GetFileName(file.FileName); string _fileExt = _fileName.Substring(_fileName.Length - 4); //Process Picture //from http://www.codeproject.com/Tips/481015/Rename-Resize-Upload-Image-ASP-NET-MVC ImageUpload imageUpload = new ImageUpload { Width = 600 }; //set Width here //Upload Picture ImageResult imageResult = imageUpload.RenameUploadFile(file); if (imageResult.Success) { //TODO: write the filename to the db //save path for image to membermedia in db MemberMedia mm = new MemberMedia(); mm.ID = (int)Member.ID; string uploadPath = config.PhotoUploadPath; mm.path = uploadPath + imageResult.ImageName; Member.MemberMedia.Add(mm); db.SaveChanges(); return("Successfully Uploaded Image"); } else { // use imageResult.ErrorMessage to show the error return("Failed to Upload Image"); } } else { return("File is empty"); } }