2012-03-01 53 views
0

我試圖上傳一個XML文件到我的網站。但是,無論我嘗試上傳哪個文件,我的代碼中的HttpPostedFileBase元素都是null。我不明白這是爲什麼。我已經遵循了我在上傳文件時可以找到的所有示例,但似乎沒有任何意義。這是控制器方法無法上傳文件ASP.NET MVC3。 XML文件到模型

[HttpPost] 
    public ActionResult UploadFile(HttpPostedFileBase xmlFile) 
    { 
     if (xmlFile != null && xmlFile.ContentLength > 0) 
     { 
      XmlDocument xmlDoc = new XmlDocument(); 
      xmlDoc.Load(xmlFile.InputStream); 
      // other logic later 
      return RedirectToAction("Index"); 
     } 
     return RedirectToAction("UploadFailed");  
    } 

和CSHTML:

@{ 
ViewBag.Title = "Upload"; 
} 

@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
<input type="file" name="file" /> 
<input type="submit" value="OK" /> 

} 

回答

2

它有一個錯誤的名稱。動作參數被稱爲xmlFile,而您的文件輸入被稱爲file。你必須要一致,您的命名約定:

<input type="file" name="xmlFile" /> 

我還請你籤菲爾哈克在這個問題上的blog post

+0

太好了。謝謝。真是愚蠢的錯誤。 – Bonnotbh 2012-03-01 22:49:44