简体   繁体   English

在jQuery Multi文件上传器中完成上传后刷新页面

[英]Refreshing the page after completing uploads in jQuery Multi file Uploader

I'm using jQuery Multifile uploader ( https://github.com/blueimp/jQuery-File-Upload ) with PHP 我正在使用PHP的jQuery Multifile上传器( https://github.com/blueimp/jQuery-File-Upload

and I want to refresh the uploads page once all files got uploaded, I'm using basic plus UI, please tell me if is there any easy way to achieve it 我想在上传所有文件后刷新上传页面,我使用的是基本加UI,请告诉我是否有任何简单的方法来实现它

Use the done and fail events along with some counters. 使用完成和失败事件以及一些计数器。 Found these events in the options documentation . 选项文档中找到这些事件。

var fileCount = 0, fails = 0, successes = 0;

$('#fileupload').fileupload({
    url: 'server/php/'
}).bind('fileuploaddone', function(e, data) {
  fileCount++;
  successes++;
  console.log('fileuploaddone');
  if (fileCount === data.getNumberOfFiles()) {
    console.log('all done, successes: ' + successes + ', fails: ' + fails);
    // refresh page
    location.reload();
  }
}).bind('fileuploadfail', function(e, data) {
  fileCount++;
  fails++;
  console.log('fileuploadfail');
  if (fileCount === data.getNumberOfFiles()) {
    console.log('all done, successes: ' + successes + ', fails: ' + fails);
    // refresh page
    location.reload();
  }
});

You can use the stop event. 您可以使用stop事件。 It is equivalent to the global ajaxStop event (but for file upload requests only). 它等同于全局ajaxStop事件(但仅适用于文件上载请求)。

stop: function(e){
      location.reload();
}

I have used this code and so far it works well. 我使用过这段代码,到目前为止效果很好。

$('#fileupload').bind('fileuploadstop', function (e) {
  console.log('Uploads finished');        
    location.reload(); // refresh page
    });

I used ryan's code, but there was a problem. 我使用了ryan的代码,但是有一个问题。 The value of data.getNumberOfFiles() was decreasing as the files were uploaded while fileCount was increasing, so my upload script got interrupted at the middle of my upload where data.getNumberOfFiles() was equal to fileCount. 当fileCount增加时,data.getNumberOfFiles()的值随着文件的上传而减少,因此我的上传脚本在我上传的中间被中断,其中data.getNumberOfFiles()等于fileCount。

Here is how i tweaked ryan's script and now it's working like a charm: 这是我如何调整ryan的脚本,现在它的工作就像一个魅力:

var fileCount = 0, fails = 0, successes = 0;
var _totalCountOfFilesToUpload = -1;

$('#fileupload').bind('fileuploaddone', function (e, data) {
    if (_totalCountOfFilesToUpload < 0) {
         _totalCountOfFilesToUpload = data.getNumberOfFiles();
    }
    fileCount++;
    successes++;
    if (fileCount === _totalCountOfFilesToUpload) {
        console.log('all done, successes: ' + successes + ', fails: ' + fails);
        // refresh page
        location.reload();
    }
}).bind('fileuploadfail', function(e, data) {
    fileCount++;
    fails++;
    if (fileCount === _totalCountOfFilesToUpload) {
        console.log('all done, successes: ' + successes + ', fails: ' + fails);
        // refresh page
        //location.reload();
    }
});

I hope this will help other people as well as! 我希望这对其他人也有帮助! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM