我正在尝试将blueimp File Upload添加到MVC应用程序中,但我在接收post action中的文件时遇到了问题(我想要多文件上传功能)。请问有人可以帮我解决这个问题吗?
在我的视图中,我有以下代码:
<form id="file_upload" enctype="multipart/form-data" action="Home/SaveFile" method="post">
<input type="file" name="file" multiple="true"/>
<button>Upload</button>
<div>Upload files</div>
</form>
<br />
###############################
<table id="files">
</table>
<button id="start_uploads">Start uploads</button>
<button id="cancel_uploads">Cancel uploads</button>
blueimp文件上传的JQuery代码如下:
$(document).ready(function () {
$('#file_upload').fileUploadUI({
uploadTable: $('#files'),
downloadTable: $('#files'),
buildUploadRow: function (files, index) {
return $('<tr><td class="file_upload_preview"><\/td>' +
'<td>' + files[index].name + '<\/td>' +
'<td class="file_upload_progress"><div><\/div><\/td>' +
'<td class="file_upload_start">' +
'<button class="ui-state-default ui-corner-all" title="Start Upload">' +
'<span class="ui-icon ui-icon-circle-arrow-e">Start Upload<\/span>' +
'<\/button><\/td>' +
'<td class="file_upload_cancel">' +
'<button class="ui-state-default ui-corner-all" title="Cancel">' +
'<span class="ui-icon ui-icon-cancel">Cancel<\/span>' +
'<\/button><\/td><\/tr>');
},
buildDownloadRow: function (file) {
return $('<tr><td>' + file.name + '<\/td><\/tr>');
},
beforeSend: function (event, files, index, xhr, handler, callBack) {
handler.uploadRow.find('.file_upload_start button').click(callBack);
}
});
$('#start_uploads').click(function () {
$('.file_upload_start button').click();
});
$('#cancel_uploads').click(function () {
$('.file_upload_cancel button').click();
});
});
在控制器中,以下是操作方法:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveFile(IEnumerable<HttpPostedFileBase> files)
{
foreach (HttpPostedFileBase file in files)
{
//some file upload magic
}
return View("MyView");
}
我正在使用MVC 3。
在动作方法中,如果参数的类型为IEnumerable,则会接收到null,如果参数的类型为HttpPostedFileBase,则以奇怪的方式接收文件,并且动作方法无法正常工作。
非常感谢任何形式的帮助。
干杯!
HttpPostedFileBase
会变成IEnumerable<HttpPostedFileBase>
吗? - Matthijs Wessels