jquery – 使用chunking进行程序化文件上传:仅发送第一个文件

前端之家收集整理的这篇文章主要介绍了jquery – 使用chunking进行程序化文件上传:仅发送第一个文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当使用 jQuery-File-Upload plugin使用分块启用的 programmatic file upload时,无法获取多个文件发送.

我打电话如下:

  1. fileUploadWidget.fileupload('send',{
  2. files: filesList
  3. })

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(你会想要这个取决于你的后端如何配置):

  1. // for illustration only
  2. // does not do anything with the returned jqXHR objects
  3. for (i = 0; i < files.length; i++) {
  4. widget.fileupload('send',{ files: files[i] });
  5. }

解决方法

如果你使用C#作为后端,你可以试试这个(Client-Side,JQuery):
  1. $("#btnUpload").click(function () { // btnUpload is the ID of any button for upload
  2. var formdata = new FormData(); //FormData object
  3. var fileInput = document.getElementById('fileInput');
  4. //Iterating through each files selected in fileInput
  5. for (i = 0; i < fileInput.files.length; i++) {
  6. //Appending each file to FormData object
  7. formdata.append(fileInput.files[i].name,fileInput.files[i]);
  8. }
  9. //Creating an XMLHttpRequest and sending
  10. var xhr = new XMLHttpRequest();
  11. xhr.open('POST','/UploadMethod'); // UploadMethod is your back-end code
  12. xhr.send(formdata);
  13. xhr.onreadystatechange = function () {
  14. if (xhr.readyState == 4 && xhr.status == 200) {
  15. // alert("uploaded");
  16. }
  17. }
  18.  
  19. });

然后在你UploadMethod上这样做:(ServerSide,C#)

  1. public void Upload()
  2. {
  3. for (int i = 0; i < Request.Files.Count; i++)
  4. {
  5. HttpPostedFileBase file = Request.Files[i]; //Uploaded file
  6. //Use the following properties to get file's name,size and MIMEType
  7. int fileSize = file.ContentLength;
  8. string fileName = file.FileName;
  9. string mimeType = file.ContentType;
  10. System.IO.Stream fileContent = file.InputStream;
  11. //To save file,use SaveAs method
  12. var appPath = HostingEnvironment.ApplicationPhysicalPath + "Documents/" + "_" + DateTime.Now+ fileName;
  13.  
  14. file.SaveAs(appPath); //File will be saved in Documents folder
  15. }
  16. }

猜你在找的jQuery相关文章