jQuery fileupload插件成功回调 [英] jQuery fileupload plugin success call back

查看:489
本文介绍了jQuery fileupload插件成功回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery File Upload Plugin(https://github.com/blueimp/jQuery-File-Upload)在rails中同时上传多张图片.照片将附加到Item模型,并将item添加到民意测验.

I used the jQuery File Upload Plugin (https://github.com/blueimp/jQuery-File-Upload) to upload multiple pictures simultaneously in rails. The photo is attached to the Item model, and item is added to a poll.

将插件用作:

$(document).ready(function(){
    $('#fileupload').fileupload({
      url: '/polls',
      dataType: "json",
      done: function(e,data){
        window.location = "/polls/" + data.result.id;
      }
    }
});

应该在每个照片文件的轮询控制器"中调用create动作,并在所有文件上传后重定向页面以显示创建的投票.然而,例如,当6个文件被选中上传,它仅在第一次显示4张照片在投票页面.刷新页面后,它显示了6张照片.我猜想问题是由回调函数引起的.似乎在上载​​所有文件之前,它已重定向到轮询页面.
完成了.所有上传请求均应成功发送,但并非所有项目都已创建(回形针可能需要几秒钟来处理图像).

It was expected to call the create action in the Poll Controller for each photo file and redirect the page to show the created poll after all the files were uploaded. However, for instance, when 6 files were chosen to upload, it only displayed 4 photos in the poll page at first. After refreshing the page, it displayed 6 photos. I guessed the issue was caused by the call-back function. Seems it redirected to the poll page before the uploading of all the files
were done. All the upload requests should be sent successfully, but the items were not all created (it may take seconds for paperclip to process the images).

对我有解决此问题的建议吗?谢谢

Any suggestions for me to solve this issue? Thanks

检查是否为每个成功的http请求实际上调用了完成"回调函数.发送所有请求后未调用它.然后为每个照片文件调用创建动作.我怎么有一个仅在所有文件上传完后才在客户端或服务器端进行回调的功能?

Checked that the "done" call-back function actually was called for each successful http request. It was not called after all the requests were sent. And the create action was called for each photo file. How can I have a function that will call back only after the all files were uploaded, either on client side or server side?

推荐答案

您可以尝试使用add回调对要上传的文件进行计数,然后在每个done回调中减少该数字.然后,只有在该号码为0时,您才进行重定向.

You could try using the add callback to count the files being uploaded and then decrement that number on each done callback. Then you would only do the redirect if that number was 0.

类似这样的东西:

$(document).ready(function(){
    var filesToProcess = 0;
    $('#fileupload').fileupload({
      url: '/polls',
      dataType: "json",
      add: function(e, data){
        filesToProcess++;
      },
      done: function(e,data){
        filesToProcess--;
        if (filesToProcess === 0){
          window.location = "/polls/" + data.result.id;
        }
      }
    }
});

这篇关于jQuery fileupload插件成功回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆