17

我正在使用 jQuery 文件上传插件。

我希望能够在所有选定文件完成上传后触发事件。So far I have an event for doing an action when a file(s) is selected for uploading and when each particular file finishes uploading - is there a way to detect that all selected files have finished uploading?

实际的应用程序在这里http://repinzle-demo.herokuapp.com/upload

我的输入字段如下所示:

<div id="fine-uploader-basic" class="btn btn-success" style="position: relative; overflow: hidden; direction: ltr;">

我的脚本代码如下所示:

<script>
        $(function () {
            $('#fileupload').fileupload({
                dataType: 'json',
                add: function (e, data) {   // when files are selected this lists the files to be uploaded
                    $.each(data.files, function (index, file) {
                        $('<p/>').text("Uploading ... "+file.name).appendTo("#fileList");
                    });
                    data.submit();
                },
                done: function (e, data) {  // when a file gets uploaded this lists the name
                    $.each(data.files, function (index, file) {
                        $('<p/>').text("Upload complete ..."+file.name).appendTo("#fileList");
                    });
                }
            });
        });
    </script>

我正在使用如下所示的 Play Framework (Java) 控制器处理请求:

公开

c static void doUpload(File[] files) throws IOException{

        List<ImageToUpload> imagesdata = new ArrayList<ImageToUpload>();
               //ImageToUpload has information about images that are going to be uploaded, name size etc.

        for(int i=0;i<files.length;i++){
            Upload upload = new Upload(files[i]);
            upload.doit(); //uploads pictures to Amazon S3
            Picture pic = new Picture(files[i].getName());
            pic.save(); // saves metadata to a MongoDB instance
            imagesdata.add(new ImageToUpload(files[i].getName()));
        }

        renderJSON(imagesdata);
    }
4

2 回答 2

21

我认为您正在寻找“停止”事件:

$('#fileupload').bind('fileuploadstop', function (e) {/* ... */})

插件文档中所述:

上传停止回调,相当于全局 ajaxStop 事件(但仅针对文件上传请求)。

您还可以在插件创建时指定函数,将其作为参数传递

$('#fileupload').fileupload({
        stop: function (e) {}, // .bind('fileuploadstop', func);
    });
于 2013-03-25T17:51:31.490 回答
0

这是您想要的解决方案:

$('#fileupload').on('fileuploaddone', function(e, data) {
      var activeUploads = $('#fileupload').fileupload('active');
      if (activeUploads == 1) {
          console.info("All uploads done");
      }
  });

我也有这个问题,3年前修复。

于 2020-10-12T19:59:11.687 回答