2013-05-13 78 views
0

我想上傳圖像,當我嘗試image.Save()時,我得到一個「通用GDI +」錯誤。任何幫助都會很棒。MVC4 - 上傳圖像 - 通用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"); 
    } 

回答

0

GDI +無法訪問「〜/ Content/Images/Specials/...」的位置,因爲它不是物理路徑。您需要使用Server.MapPath()將虛擬路徑映射到物理路徑。

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

而且,它的怪異,你串聯路徑的一部分,那麼「rrest」,然後「.JPG」。在投入生產之前,可能需要考慮這一點,以免一遍又一遍地覆蓋圖像。