我正在尝试添加
blueimp File Upload到一个MVC应用程序,我有问题在接收文件的后期操作(即将进行多文件上传功能).有人可以帮我看出来吗?
在我看来,我有以下代码:
<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>
$(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.
在action方法中,如果参数类型为IEnumerable,则它将接收null,如果参数的类型为HttpPostedFileBase,则以奇怪的方式接收文件,并且action方法无法正常运行.
非常感谢任何一种帮助,谢谢.
干杯!
解决方法
将为每个文件调用SaveFile控制器操作.所以应该是这样的:
[HttpPost] public ActionResult SaveFile(HttpPostedFileBase file) { //some file upload magic // return JSON return Json(new { name = "picture.jpg",type = "image/jpeg",size = 123456789 }); }
如果您想在同一个请求中处理多个上传文件,可以查看respective section of the documentation.