0

我正在尝试上传图像,当我尝试 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");
    }
4

1 回答 1

0

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

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

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

于 2013-05-13T04:03:30.130 回答