User-1244695925 posted
Uploading and resizing an image... works great on local dev box. Fails on host with the exception: A generic error occurred in GDI+. In my ftp client, Filezilla, I set the directory permissions to "777" for the directory the image is being uploaded
to. Which should be enough to allow .NET to save files to the directory right?
Here's the code:
1 // save file
2 var posted = Request.Files["Photo"];
3 if (posted.ContentLength > 0)
4 {
5 // check if it is a jpg
6 if (Path.GetExtension(posted.FileName).ToLower() != ".jpg")
7 {
8 ModelState.AddModelError("Photo", "Photo must be a .jpg");
9 throw new InvalidOperationException();
10 }
11
12 // rename the file
13 string filename = DateTime.Now.ToUniversalTime().ToString().Replace("/", "-").Replace(":", "-") + "-" + Path.GetFileName(posted.FileName);
14 ad.Photo = filename;
15
16 // Get the bitmap data from the uploaded file
17 Bitmap src = Bitmap.FromStream(posted.InputStream) as Bitmap;
18
19 // Resize the bitmap data
20 Bitmap result = ModelHelpers.ProportionallyResizeBitmap(src, 640, 480);
21
22 // set the path to save
23 string savePath = "/Content/UsrImg/";
24 string saveName = Server.MapPath(savePath) + filename;
25
26 // save
27 result.Save(saveName, ImageFormat.Jpeg);
28 }
Line 20 just takes the bitmap, does a resize, and returns it. If you think this code could be the culprit, I can post it as well.
Here's the stack trace:
1 [ExternalException (0x80004005): A generic error occurred in GDI+.]
2 System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +397778
3 System.Drawing.Image.Save(String filename, ImageFormat format) +69
4 SomethingNS.Controllers.SomethingController.New(FormCollection form, String b) in \Visual Studio 2008\Projects\Controllers\SomethingController.cs:116
5 lambda_method(ExecutionScope , ControllerBase , Object[] ) +127
6 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
7 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +266
8 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +23
9 System.Web.Mvc.<>c__DisplayClassa.<invokeactionmethodwithfilters>b__7() +110
10 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +297
11 System.Web.Mvc.<>c__DisplayClassc.<invokeactionmethodwithfilters>b__9() +20
12 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +215
13 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +312
14 System.Web.Mvc.Controller.ExecuteCore() +176
15 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +20
16 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +4
17 System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +119
18 System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +36
19 System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +4
20 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +358
21 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64</invokeactionmethodwithfilters></invokeactionmethodwithfilters>
Any ideas are greatly appreciated.