2

This is my first question. Thanks to everyone who contributes to this site, it's been a huge help as I try to teach myself programming.

I'm having a problem saving an uploaded image. I think I'm doing something wrong with the image.save function. My code is as follows:

Controller:

[AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(string fileName, HttpPostedFileBase pic) { string mimeType = pic.ContentType; byte[] imagedata = new byte[pic.ContentLength]; pic.InputStream.Read(imagedata, 0, pic.ContentLength); Image outfitImage = (Image.FromStream(new MemoryStream(imagedata))); string filePath = @"c:\test.jpg"; outfitImage.Save(filePath); return View(); } 

View:

<% using (Html.BeginForm("Index","Test" ,FormMethod.Post,new {enctype="multipart/form-data"})) {%> <fieldset> <legend>Fields</legend> <p> <label for="fileName">Name:</label> <%= Html.TextBox("fileName") %> </p> <p> Upload new image: <input type="file" name="pic" /> </p> <p> <input type="submit" value="Create" /> </p> </fieldset> <% } %> 

The stack trace is as follows:

 Server Error in '/' Application. A generic error occurred in GDI+. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. Source Error: Line 51: Line 52: string filePath = @"c:\test.jpg"; Line 53: outfitImage.Save(filePath); Line 54: Line 55: return View(); Source File: C:\Users\Solomon\SolomonApp\AmIStylin\AmIStylin\Controllers\TestController.cs Line: 53 Stack Trace: [ExternalException (0x80004005): A generic error occurred in GDI+.] System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +377630 System.Drawing.Image.Save(String filename, ImageFormat format) +69 System.Drawing.Image.Save(String filename) +25 AmIStylin.Controllers.TestController.Index(String fileName, HttpPostedFileBase pic) in C:\Users\Solomon\SolomonApp\AmIStylin\AmIStylin\Controllers\TestController.cs:53 lambda_method(ExecutionScope , ControllerBase , Object[] ) +205 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24 System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() +52 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254 System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +192 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +399 System.Web.Mvc.Controller.ExecuteCore() +126 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +27 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7 System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +151 System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +57 System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75 Version Information: Microsoft .NET Framework Version:2.0.50727.4062; ASP.NET Version:2.0.50727.3074 

Thanks so much for the help!

2
  • You should be calling pic.InputStream.Read in a loop, as it's not guaranteed to read it all at once. (Check its return value) Commented Feb 24, 2010 at 4:17
  • Thanks SLaks. How exactly would I do this? Commented Feb 24, 2010 at 16:24

2 Answers 2

2

Are you trying to convert the image to a JPEG?

If not, you should just call pic.Save(@"C:\text.jpg").

You might be getting the error because ASP.Net does not have write access to the root of the C: drive. (GDI+ can give funny errors)
How and where is this code being run? (Cassini? IIS?)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I tried hardcoding the filepath, but got the same error. The code is being run on my local machine (the good old F5 in visual studio), so I think it's IIS. I would like the pic to be a JPEG.
If you're pressing F5 in VS, you're running Cassini.
Try changing it to pic.Save(@"C:\text.jpg") and see whether it gives an error.
SLaks, you were right, it was a permission issue. I changed to filepath to a sub-directory of my app, and it worked. Thanks so much!
2

I would just figure out where you want to store it relative to the web server, translate that to the file server, and then save it.

string relativePath = "~/somefolder/somefile.ext"; string diskPath = ControllerContext.HttpContext.Server.MapPath(newFileWebPath); pic.SaveAs(diskPath); 

3 Comments

Unless he wants to convert it to jpeg.
Thanks! My hosting provider (rackspace) is Medium Trust, and I need to use absolute paths, not relative paths though when I deploy...
Server.MapPath returns an absolute path. This code should work, assuming that you have write access to the path. (Which you probably don't)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.