2014-05-11 1 views
1

내가 내 HomeControllerASP MVC HttpPostedFileBase는 항상 null

[HttpPost] 
public ActionResult NewRequest(int? id, FormCollection form, HttpPostedFileBase uploadFile) 
{ 
    if (uploadFile != null && uploadFile.ContentLength > 0) 
     { 
      // extract only the fielname 
      var fileName = Path.GetFileName(uploadFile.FileName); 
      // store the file inside ~/App_Data/uploads folder 
      var path = Path.Combine(Server.MapPath("~/Documents/Files"), fileName); 
      uploadFile.SaveAs(path); 
     } 
     // redirect back to the index action to show the form once again 
     return RedirectToAction("Index");  
} 

이 코드가 그리고 내 NewRequest보기 문제는이

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

입니다 반환 업로드 된 파일, .txt 파일의 f.x. 항상 null입니다.

답변

0

주어진 코드가 정상입니다. 내가 다르게 생각할 수있는 유일한 경우는 라우트 구성이 맞지 않을 것입니다.

이 구성으로 테스트 해 보았는데 매력적이었습니다.

 routes.MapRoute(
      name: "NewRequest", 
      url: "Controllers/NewRequest", 
      defaults: new { controller = "Controllers", action = "NewRequest" } 
      );