2014-11-18 5 views
0

MVC 및 jQuery에서 파일 업로드를 수행하려고하지만 내 paremter가 null입니다. 내 jQuery에서 양식 데이터를 가져 와서 변수에 할당 한 다음 ActionResult에 게시합니다. 내 매개 변수가 null이고 데이터를 올바르게 전달하는지 잘 모르겠습니다.POST 이미지 파일 - ActionResult의 매개 변수가 null입니다.

HTML

<table> 
       <tr> 
        <td><img src="@Url.Content("~/AMTImages/UserAvatar.png")" width="64" height="64" style="border: thin solid" /></td> 
        <td></td> 
        <td></td> 
        <td> 
         <form enctype="multipart/form-data" id="imageUploadForm"> 
          <table> 
           <tr> 
            <td>@Html.LabelFor(m => m.DisplayName, "Display Name: ")</td> 
            <td style="padding-left: 10px;">@Html.TextBoxFor(m => m.DisplayName)</td> 
           </tr> 
           <tr> 
            <td>@Html.LabelFor(m => m.UserImage, "User Image: ")</td> 
            <td style="padding-left: 10px; float: right;">@Html.TextBoxFor(m => m.UserImage, new { type = "file", accept = "image/*" })</td> 
           </tr> 
          </table> 
         </form> 
        </td> 
        <td> 
         <table style="display:none;"> 
          <tr> 
           <td> 
            <progress></progress> 
           </td> 
          </tr> 
         </table> 
        </td> 
       </tr> 
      </table> 

자바 스크립트

$(':file').change(function() { 
    var file = this.files[0]; 

    $.ajax({ 
     url: '@Url.Action("UploadImage", "User")', //Server script to process data 
     type: 'POST', 
     data: file, 
     beforeSend: function() { 
     }, 
     success: function() { 
     }, 
     error: function() { 
     }, 
     processData: false, 
     contentType: file.type 
    }); 
}); 

ActionResult는

public ActionResult UploadImage(HttpPostedFileBase file) 
     { 
      if (file != null && file.ContentLength > 0) 
       try 
       { 
        string path = Path.Combine(Server.MapPath("~/Images"), 
               Path.GetFileName(file.FileName)); 
        file.SaveAs(path); 
        ViewBag.Message = "File uploaded successfully"; 
       } 
       catch (Exception ex) 
       { 
        ViewBag.Message = "ERROR:" + ex.Message.ToString(); 
       } 
      else 
      { 
       ViewBag.Message = "You have not specified a file."; 
      } 

      return View(); 
     } 
+0

@Rory McCrossan - 내 게시물은 ASP.NET MVC에 관한하고 그 답은 클라이언트 코드를 보여줍니다. –

+0

맞습니다. 여기에있는 문제는 클라이언트 쪽 코드로 인해서 내가 솔루션에 연결된 질문과 동일합니다. –

답변

0

귀하의 UploadImage는 파일을 기대 POST 매개 변수. 프런트 엔드 코드에서 그 부분을 어디에 지정합니까? 어디로 지나가는지 알 수 없습니다.

시도 HttpPostedFileBase userImage에 HttpPostedFileBase 파일을 변경

+0

실제로 데이터를 전달하지 않고 있지만 여전히 null 인 것으로 나타났습니다. 난 그냥 내 매개 변수가 C#에서 어떤 데이터 형식인지 알아야합니다. 내 SO 게시물을 새로운 javaScript로 업데이트했습니다. –

관련 문제