当使用
jQuery-File-Upload plugin使用分块启用的
programmatic file upload时,无法获取多个文件发送.
我打电话如下:
fileUploadWidget.fileupload('send',{ files: filesList })
filesList是File对象的列表.
另外我已经设置了maxChunkSize,并将singleFileUploads设置为true(我也尝试过false),如Options wiki page所示.
有没有人有这样的工作成功?
更新:
我在GitHub上做了一个issue这个问题,这里是作者的回应:
[…] Chunked uploads only support one file per request.
That is,you can still simultaneously upload multiple files in chunks,but you’ll have to send mutliple requests for that.
我们的解决方案
正如已经评论过的那样,我们最终做的是将文件发送到一个循环,在这个循环中,widget被初始化为sequentialUploads设置为true(你会想要这个取决于你的后端如何配置):
// for illustration only // does not do anything with the returned jqXHR objects for (i = 0; i < files.length; i++) { widget.fileupload('send',{ files: files[i] }); }
解决方法
如果你使用C#作为后端,你可以试试这个(Client-Side,JQuery):
$("#btnUpload").click(function () { // btnUpload is the ID of any button for upload var formdata = new FormData(); //FormData object var fileInput = document.getElementById('fileInput'); //Iterating through each files selected in fileInput for (i = 0; i < fileInput.files.length; i++) { //Appending each file to FormData object formdata.append(fileInput.files[i].name,fileInput.files[i]); } //Creating an XMLHttpRequest and sending var xhr = new XMLHttpRequest(); xhr.open('POST','/UploadMethod'); // UploadMethod is your back-end code xhr.send(formdata); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { // alert("uploaded"); } } });
然后在你UploadMethod上这样做:(ServerSide,C#)
public void Upload() { for (int i = 0; i < Request.Files.Count; i++) { HttpPostedFileBase file = Request.Files[i]; //Uploaded file //Use the following properties to get file's name,size and MIMEType int fileSize = file.ContentLength; string fileName = file.FileName; string mimeType = file.ContentType; System.IO.Stream fileContent = file.InputStream; //To save file,use SaveAs method var appPath = HostingEnvironment.ApplicationPhysicalPath + "Documents/" + "_" + DateTime.Now+ fileName; file.SaveAs(appPath); //File will be saved in Documents folder } }