2012-12-13 3 views
0

uploadify를 사용하여 컨트롤러 메서드에서 사용자 파일을 자동 제출합니다. Request.Files [ "Name"]은 null을 계속 반환하지만 request.form은 null이 아니며, 내가 breakpoint를 설정하고 그것을 디버깅 할 때 request.form에있는 파일. 내가 놓친 게 있니? 나는 이것을 mvc2에서 테스트하고 있지만 mvc4에서 사용할 계획이다.Request.Files [ ""] null을 계속 반환합니다.

<link href="../../Content/uploadify.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script> 
<script src="../../Scripts/jquery.uploadify.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(function() { 
     $('#file_upload').uploadify({ 
      'swf': '/Content/uploadify.swf', 
      'uploader': '/Home/UploadFile', 
      'auto': true 


      // Your options here 
     }); 
    }); 
</script> 
</head> 
    <body>   
    <%--<% using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, 
new { enctype = "multipart/form-data" })) 
{ %>--%> 
    <input type="file" name="file_upload" id="file_upload" style="margin-bottom: 0px" /> 

<%-- <% } %>--%> 

컨트롤러 방법 : 내가 제출 버튼을 추가 한 다음 제출하면

[HttpPost] 
    public ActionResult UploadFile(HttpPostedFileBase file) 
    {   
     var theFile = Request.Files["file_upload"]; 
     return Json("Success", JsonRequestBehavior.AllowGet); 
    } 

그래도 작동합니다. 제출 버튼없이 자동으로해야합니다.

+0

저는 uploadify에 익숙하지 않습니다. 그러나 폼이있을 때 명시 적으로 POST 메서드를 사용하고 작업 메서드는 POST 요청에 응답하는 것처럼 보입니다. 업로드가 GET을 할 수 있습니까 ??? –

답변

2

IIRC UploadifyfileData을 매개 변수로 사용합니다. 그래서 :

var theFile = Request.Files["fileData"]; 

또는 더 나은 :

물론
[HttpPost] 
public ActionResult UploadFile(HttpPostedFileBase fileData) 
{   
    // The fileData parameter should normally contain the uploaded file 
    // so you don't need to be looking for it in Request.Files 
    return Json("Success", JsonRequestBehavior.AllowGet); 
} 

이 이름에 만족하지 않는 경우 당신은 항상 fileObjName 설정을 사용하여 사용자 정의 할 수 있습니다.

+0

아아 네 오른쪽. 감사. 자습서에서 그 일을해야한다는 것에 대해서는 아무 것도 보지 못했습니다. 고맙습니다. 왜 이런 식으로? – TMan

+0

글쎄, 그들은 파일을 보내려면 몇 가지 기본값을 선택해야 했지, 그렇지? 왜 그들이'foo_bar_baz' 대신에'fileData'를 선택했는지는 대답하기 어려운 질문입니다. ['multipart/form-data'] (http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2) 명세에서 이미 알 수 있듯이 (의심 할 여지없이 이미 읽고 웹 개발을하는 경우 익숙합니다.) 업로드 된 각 파일은 서버가 요청에서 검색하는 데 사용할 수있는 고유 한 키와 연결됩니다. –

+0

네, 저는 웹 개발에서 파일 업로드에 익숙하지 않습니다. 나는 더 해협 인 데스크톱 개발에서이 작업을 수행했습니다. – TMan

관련 문제