问题
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
回答1:
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.
回答2:
First of all, I am not sure why you have this three times.
@Html.Partial("_UploadItem")
@Html.Partial("_UploadItem")
@Html.Partial("_UploadItem")
Are you trying to upload multiple files? There are quite a few scripts that allow you to do this quite efficiently. You are also generating multiple forms with fields that contain the same id. This wont effect the upload process, but is bad practice. I do think you need to refactor the above. Anyway change the field in your partial view to
<input type="file" name="file" />
I think the problem may be with your actual action and how you are trying to retrieve the file. Try this
HttpPostedFileBase postedFile = Request.Files["file"];
Let me know if that works.
来源:https://stackoverflow.com/questions/8499748/mvc3-file-upload-using-ajax-form-request-files-empty