【问题标题】:MVC4 - Uploading Image - Generic GDI+ ErrorMVC4 - 上传图像 - 通用 GDI+ 错误
【发布时间】:2013-05-07 01:03:48
【问题描述】:

我正在尝试上传图像,当我尝试 image.Save() 时,我收到“通用 GDI+”错误。任何帮助都会很棒。

    // This action handles the form POST and the upload
    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file, string filename)
    {
        // Verify that the user selected a file
        if (file != null && file.ContentLength > 0)
        {                
            string location = "~/Content/Images/Specials/" + "rttest" + ".jpg";
            Bitmap bitmap = new Bitmap(file.InputStream);
            Image image = (Image)bitmap;
            image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            image.Save(location);            
        }
        // redirect back to the index action to show the form once again
        return RedirectToAction("Index");
    }

【问题讨论】:

    标签: asp.net-mvc image file-upload gdi


    【解决方案1】:

    GDI+ 无法访问“~/Content/Images/Specials/...”的位置,因为它不是物理路径。您需要使用Server.MapPath() 将虚拟路径映射到物理路径。

     string location = Server.MapPath("~/Content/Images/Specials/" + "rttest" + ".jpg");
    

    此外,您将路径的一部分连接起来,然后是“rrest”然后是“.jpg”,这很奇怪。可能需要在将其投入生产之前对其进行调查,这样您就不会一遍又一遍地覆盖您的图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 2017-08-18
      • 1970-01-01
      • 2012-06-27
      相关资源
      最近更新 更多