mvc3 file upload using ajax form - Request.Files empty

Rick Hodder picture Rick Hodder · Dec 14, 2011 · Viewed 14.8k times · Source

Im using mvc3, and am trying to create a series of ajax forms that each upload a file, within a single page. Here's the view for the page:

@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>

    @Html.Partial("_UploadItem")
    @Html.Partial("_UploadItem")
    @Html.Partial("_UploadItem")

<script type="text/javascript">
    function Go() {
        // loop through form tags
        for (var n = 0; n < document.forms.length; n++) {
            var f = document.forms[n];
            // if a dress is chosen, a caption is chosen
            // and a file is chosen, then submit the ajax form
            if (f.dressid.value != '' &&
               f.dresscaption.value != '' &&
               f.fileitem.value != '')
                f.submit();
        }
    }
</script>
<input type="button" onclick="Go();"/>

Then the Go() function loops through the ajax forms, checking to see whether all 3 pieces (dressid, dresscaption, and fileitem) on each form are non-empty, and calls submits the forms that do, kicking off an async upload.

Here's the partial view:

@using SoRefeising.Models
@using (Ajax.BeginForm("UploadFile", new { }, new AjaxOptions { HttpMethod = "POST" }, new { enctype="multipart/form-data"}))
{
    List<SelectListItem> items = (List<SelectListItem>)ViewBag.Dresses;

    <span>Dress</span>
    @Html.DropDownList("dressid", items, "Choose Dress");
    <span>Caption</span>
    @Html.TextBox("dresscaption")
    <input type="file" id="fileitem" />    
}

I have marked each form with the multipart attribute. When the page is generated, I get the following:

<form action="/upload/UploadFile" data-ajax="true" data-ajax-method="POST" enctype="multipart/form-data" id="form0" method="post">    <span>Dress</span>
<select id="dressid" name="dressid"><option value="">Choose Dress</option>
<option value="1">Simpson01</option>
<option value="2">Simpson02</option>
</select>    <span>Caption</span>
<input id="dresscaption" name="dresscaption" type="text" value="" />    <input type="file" id="fileitem" />    
</form>

    <form action="/upload/UploadFile" data-ajax="true" data-ajax-method="POST" enctype="multipart/form-data" id="form1" method="post">    <span>Dress</span>
<select id="dressid" name="dressid"><option value="">Choose Dress</option>
<option value="1">Simpson01</option>
<option value="2">Simpson02</option>
</select>    <span>Caption</span>
<input id="dresscaption" name="dresscaption" type="text" value="" />    <input type="file" id="fileitem" />    
</form>

    <form action="/upload/UploadFile" data-ajax="true" data-ajax-method="POST" enctype="multipart/form-data" id="form2" method="post">    <span>Dress</span>
<select id="dressid" name="dressid"><option value="">Choose Dress</option>
<option value="1">Simpson01</option>
<option value="2">Simpson02</option>
</select>    <span>Caption</span>
<input id="dresscaption" name="dresscaption" type="text" value="" />    <input type="file" id="fileitem" />    
</form>

Everything looks ok...

Here's the controller action that is called

[HttpPost]
        public ActionResult UploadFile(string dressid, string dresscaption)
        {
                HttpPostedFileBase hpf = Request.Files[0] as HttpPostedFileBase;
...

When the action is called, the Request.Files collection has 0 items, rather than the selected file. I have enabled unobtrusive javascript, the unobtrusive file is loaded in the master page, and works on other pages.

Ive seen some messages on the forum about being careful with file sizes. The file I am testing with is < 2k

Any ideas why there are no items in Request.Files?

Thanks

Answer

Darin Dimitrov picture Darin Dimitrov · Dec 14, 2011

You cannot upload files using AJAX. So replace the Ajax.BeginForm with a normal Html.BeginForm. You may checkout the following blog post as well.

If you want to use asynchronous uploads you may try some of the available upload components such as Ajax Upload and Uploadify.