2012-03-01 2 views
0

내 사이트에 XML 파일을 업로드하려고합니다. 그러나 업로드 할 파일에 관계없이 내 코드의 HttpPostedFileBase 요소는 null입니다. 나는 이것이 왜 있는지 이해하지 못한다. 나는 파일을 업로드 할 때 찾을 수있는 모든 예를 따라 왔으며 아무런 의미가없는 것처럼 보입니다.파일 업로드에 문제가 발생했습니다. ASP.NET MVC3. XML 파일을 모델

@{ 
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

그것은 잘못된 이름이이 컨트롤러 방법

[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입니다. 작업 인수는 xmlFile이고 파일 입력은 file입니다. 당신은 당신의 이름에 대한 규정이 일치해야합니다

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

나는 또한 당신이 체크 아웃을 초대 필 Haack의 blog post이 주제에.

+0

좋아요. 감사. 정말 어리석은 실수. – Bonnotbh

관련 문제