【问题标题】:Blueimp File Upload: file is not deleting correctly from queueBlueimp 文件上传:文件未从队列中正确删除
【发布时间】:2014-01-25 18:06:13
【问题描述】:

我有以下 blueimp 文件上传代码:

.on('fileuploadadd', function (e, data) {
   data.context = $('<div/>').appendTo('#FilesListThumb');
   $.each(data.files, function (index, file) {
         var node = $("<div><h6 fileId="+index+">X</h6></div>").addClass("thumbnail-ib");
         node.appendTo(data.context);
         node.find("h6").click(function(){
              data.files.splice($(this).attr("fileId"),1);
              node.remove();
         });
 });

当我单击h6 时,有时它会从队列中删除文件,有时不会。为什么?下一个问题是如何从队列中删除所有文件?

【问题讨论】:

    标签: jquery jquery-file-upload blueimp


    【解决方案1】:

    我建议你使用递增的全局变量而不是索引值:

    var globalVariable = 0; // To be declare in a global context
    
    .on('fileuploadadd', function (e, data) {
        data.context = $('<div/>').appendTo('#FilesListThumb');
        $.each(data.files, function (index, file) {
            globalVariable = globalVariable + 1;
            var node = $("<div><h6 fileId=" + globalVariable + ">X</h6></div>").addClass("thumbnail-ib");
            file.fileId = globalVariable; // Add the same id to the file
            node.appendTo(data.context);
            node.find("h6").click(function(){
                $.each(data.files, function (index, file) {
                    if(file.fileId == $(this).attr("fileId"){
                         data.files.splice(index, 1);
                         return false; // Break the loop
                };
                node.remove();
            });
        });
    });
    

    要从队列中删除文件,您可以查看关于 SO 的以下问题(我喜欢 tanguy_k 的回答):How do I empty an array in JavaScript? 并将其应用于 data.files 数组。

    【讨论】:

    • 我正在使用类似 `node.find("h6").on("click",function(){ for(var i=0;i
    • @Manish 它假定你所有的文件都有不同的名字。
    • 不,可能是相同的文件名
    • 因此您应该使用其他没有冲突的 id,例如自动增量 id,或散列组合值(名称 + 时间戳)。我上面的例子是一个自动递增的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2015-01-29
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    相关资源
    最近更新 更多