2013-12-21 5 views
0

내 MVC 응용 프로그램에서 파일 업로드 허용을 업로드하려고하지만 페이지를 테스트 할 때 단순히 새로 고치거나 오류를 표시하지 않으며 파일을 업로드하지 않기 때문에 무엇이 잘못 되었는가에 관해 완전한 루스에서.파일 업로드가 작동하지 않지만 오류가 발생하지 않음

보기 :

<form action="" method="post" enctype="multipart/form-data"> 

    <label for="file">Filenname:</label> 
    <input type="file" name="file" id="file" /> 
    <input type="submit" /> 
</form> 

컨트롤러 :

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

     return RedirectToAction("Index"); 
    } 
+1

양식 조치가 비어 – Jasen

+0

나는 다음과 같은 한 @Jasen http://haacked.com/archive/2010/07/16/uploading-files- with-aspnetmvc.aspx/양식 작업은 무엇이되어야합니까? 일반적으로 ""의 동작은 같은 페이지에 게시하는 것을 의미하며 인덱스 작업 결과를 가져와야합니다 – Matthew

+1

Jasen이 맞습니다. 액션이 진행될 곳을 지정해야합니다. BeginForm –

답변

2

시도 :

<form action="Controller/Index" method="post" enctype="multipart/form-data"> 

    <label for="file">Filenname:</label> 
    <input type="file" name="file" id="file" /> 
    <input type="submit" /> 
</form> 
+0

이것은 컨트롤러 밖에서 작동하므로'action = "Index"' – Matthew

0

실제로 그것을 시도하지 않은,하지만 내 생각 엔 당신이 Html.BeginForm를 사용하지 않는 당신은 단순히 <form>을 사용하고 있기 때문에이 점이다. 이 시도 :

@using (Html.BeginForm("Index","Upload",FormMethod.Post, new {enctype="multipart/form-data"})) 
{ 
    <input type="file" name ="file" id ="file"/> 
    <input type="submit"> 
} 
관련 문제