2013-01-12 43 views
0

在本地機器上,下面的代碼正常工作,但是當託管在遠程服務器上時,它似乎沒有找到圖像文件。我創建了圖像文件夾並賦予它讀取/寫入權限在ASP.NET MVC 3.0中上傳圖像時出現錯誤

// ------------------------------ --------------
錯誤

Server Error in '/' Application. 
Could not find file 'IdImage.jpg'. 

描述:在當前web請求的執行過程中發生了未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

Exception Details: System.IO.FileNotFoundException: Could not find file 'IdImage.jpg'.

代碼是如下
// --------------------------------- ------------

[HttpPost] 
public ActionResult Index(HttpPostedFileBase file) 
{ 
    if (file != null && file.ContentLength > 0) 
    { 
     var fileName = Path.GetFileName(file.FileName); 
     var path = Path.Combine(Server.MapPath("~/Images/Photo"), fileName); 
     file.SaveAs(path); 
    } 

    return RedirectToAction("Index");   
} 

回答

0

這看起來非常像許可問題。你說你已經創建了Images文件夾並給了它讀/寫權限,但是從我在代碼中可以看到的內容中,您試圖將該映像存儲在名爲Photo的子文件夾中。所以確保你已經創建了這個文件夾,並給予它的讀/寫權限。另外請確保您將這些權限授予正確的帳戶 - 在IIS中配置的帳戶以運行託管應用程序的應用程序池。

0

我使用此代碼

[HttpPost] 
public ActionResult Upload(System.Web.HttpPostedFileBase file) 
{ 
     string filename = Server.MapPath("~/files/somename"); 
     file.SaveAs(filename); 
     return RedirectToAction("Index"); 
} 

鑑於

@{ 
    ViewBag.Title = "Upload"; 
} 
<h2> 
    Upload</h2> 
@using (Html.BeginForm(actionName: "Upload", controllerName: "User", 
         method: FormMethod.Post, 
         htmlAttributes: new { enctype = "multipart/form-data" })) 
{ 
    <text>Upload a photo:</text> <input type="file" name="photo" /> 
    <input type="submit" value="Upload" /> 
} 
相關問題