【问题标题】:Problem saving an uploaded image保存上传的图像时出现问题
【发布时间】:2010-02-24 13:55:54
【问题描述】:

这是我的第一个问题。感谢所有为本网站做出贡献的人,在我尝试自学编程时,这给了我很大的帮助。

我在保存上传的图片时遇到问题。我认为我在 image.save 功能上做错了。我的代码如下:

控制器:

[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();
    }

查看:

<% 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>
<% } %>

堆栈跟踪如下:

    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 

非常感谢您的帮助!

【问题讨论】:

  • 您应该循环调用pic.InputStream.Read,因为不能保证一次全部读取。 (检查它的返回值)
  • 感谢 SLaks。我该怎么做呢?

标签: c# asp.net-mvc


【解决方案1】:

您是否尝试将图像转换为 JPEG?

如果没有,你应该打电话给pic.Save(@"C:\text.jpg")

您可能会收到错误消息,因为 ASP.Net 没有对 C: 驱动器根目录的写入权限。 (GDI+ 可以给出有趣的错误)
这段代码是如何以及在哪里运行的? (卡西尼?IIS?)

【讨论】:

  • 谢谢!我尝试硬编码文件路径,但得到了同样的错误。该代码正在我的本地机器上运行(Visual Studio 中的旧 F5),所以我认为它是 IIS。我希望图片是 JPEG。
  • 如果你在 VS 中按 F5,你正在运行 Cassini。
  • 试试改成pic.Save(@"C:\text.jpg")看看有没有报错。
  • SLaks,你是对的,这是一个权限问题。我将文件路径更改为我的应用程序的子目录,并且它有效。非常感谢!
【解决方案2】:

我只想找出相对于 Web 服务器的存储位置,将其转​​换为文件服务器,然后保存。

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

【讨论】:

  • 除非他想把它转换成 jpeg。
  • 谢谢!我的托管服务提供商(机架空间)是中等信任度,我需要使用绝对路径,而不是在部署时使用相对路径...
  • Server.MapPath 返回一个绝对路径。假设您对路径具有写访问权限,此代码应该可以工作。 (你可能不知道)
【解决方案3】:

【讨论】:

  • 这对他一点帮助都没有。
猜你喜欢
  • 2022-01-14
  • 2019-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-26
  • 2018-05-22
  • 2013-06-12
相关资源
最近更新 更多