Search code examples
c#htmlasp.net-mvcuploadify

Request.Files[""] Keep returning null


Using uploadify to auto submit a users files, in my controller method Request.Files["Name"] keeps returning null but request.form isn't null, I can see the file in request.form when I set a breakpoint and debug it. Am I missing something? I'm testing this on mvc2 but i plan on using it on mvc4.

<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" />

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

Controller Method:

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

If I add a submit button and then submit it it will work though. I need to be auto though without a submit button.


Solution

  • IIRC Uploadify uses fileData as parameter. So:

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

    or even better:

    [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);
    }
    

    Of course if you are not happy with this name you could always customize it using the fileObjName setting.