2012-10-05 3 views
1

파일을 업로드해야하며 몇 가지 방법이 있습니다. 나는 그것을 생각하는 가장 좋은 방법은이 블로그에 따라되었습니다 Blogasp.net mvc3에서 파일 업로드

는 또한 여기에 유용한 게시물을 발견 : stackoverflow topic

을하지만 그것을하려고하면 실패합니다. Visual Studio에서 다음과 같은 오류가 발생합니다.

시퀀스에 두 개 이상의 요소가 포함되어 있습니다.

컨트롤러 :

public PartialViewResult Index(parameterlist) 
    { 
     var model = new Model(); 


     return PartialView(model); 
    } 

[HttpPost] 
    public ActionResult Index(HttpPostedFileBase file) 
    { 
     // Verify that the user selected a file 
     if (file != null && file.ContentLength > 0) 
     { 
      // extract only the filename 
      var fileName = Path.GetFileName(file.FileName); 

      var path =   Path.Combine(Server.MapPath(@"Path"), fileName); 
      file.SaveAs(path); 
     } 
     // redirect back to the index action to show the form once again 
     return RedirectToAction("Index");    
    } 

보기 :

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

그래서 partialview이는 actionResult 방법에 간다 호출되는 순간, 당연히 어떤이

내 코드는 다음과 같습니다 보기가 아직 열리지 않았으므로 파일을 선택했습니다. 보기에 다른 텍스트 상자와 드롭 다운이 있습니다. 그러나 특별한 것은 없습니다. 누군가 내가 잘못하고있는 것이 무엇인지에 대한 생각을 가지고 있습니까? 내가 뭔가 중요한 결정을 놓치고있어 느낌이있어 ...

+0

스택 추적 또는 글자 그대로 예외 메시지를 게시 할 수 있습니까? – Rob

+0

모두 괜찮아 보이지만,이 구현을 피하려면 Microsoft.Web.Helpers 네임 스페이스를 사용할 수 있습니다. 여기에는 FileUpload 클래스가 포함되어 있기 때문에 사용할 수 있습니다. 예 : http://msdn.microsoft.com/en-us/library/microsoft.web.helpers%28v=vs.99%29.aspx – Freeman

+0

Ok 예외가 발생하는 이유를 발견 했으므로 매개 변수 목록을 다음과 같이 전달해야했습니다. 내가 RedirectToAction ("Index")을했을 때 그러나 지금은 페이지가로드되지만 파일을 업로드하려고 시도 할 때 페이지의 다른 버튼의 기능을 실행합니다. –

답변

1

잘못된 기능을 실행하지 않으려면 Html.BeginForm에 컨트롤러와 동작을 지정한 null 값 대신 지정해야합니다.

@using (Html.BeginForm("Index", "YourController", FormMethod.Post, ... 
+0

이제 내보기의 문제를 알았습니다. 나는 형태로 형태를 가지고있다. 업로드 된 이미지를 다른 세부 정보와 함께 제출해야합니다. 그러나 그 세부 사항을 입력하기 전에 먼저 파일을 업로드해야합니다. –

+0

도 언급해야합니다. 어쨌든 그들을 분리하고 분류 한 것처럼 보입니다. – dove

0

나는 한 형태로 병합 시도, 그래서 내 양식은 이제 다음과 같습니다

@using (Ajax.BeginForm("Method", "Controller",new { enctype = "multipart/form-data" }, new AjaxOptions { HttpMethod = "Post", OnSuccess = "Method" }, new { @class = "css", id = "formname" })) 
{ 
    Some divs and stuff 

    <input type="file" name="file" class="btn btn-inverse" /> 
    </div> 

기능은 다음과 같습니다 :

[HttpPost] 
     public void _SaveDocument(Model model, HttpPostedFileBase file) 
     { 
      if (file != null && file.ContentLength > 0) 
      { 
       var fileName = Path.GetFileName(file.FileName); 
       var path = Path.Combine(Server.MapPath(@"Path"), fileName); 
       file.SaveAs(path); 

       var command = new command() 
       { 
        Model = model 
       }; 
       commandDispatcher.Dispatch(command); 
      }    
     } 

그러나 파일이 그것을 추가되지 없구요 항상 null입니다.

+0

ajax를 사용하여 파일을 업로드 할 수 없습니다. 이를 위해 Ajax Upload 또는 Uploadify와 같은 특수 컴포넌트를 살펴볼 필요가 있습니다. – dove