【问题标题】:Delete files programmatically with jquery fileupload basic使用 jquery fileupload basic 以编程方式删除文件
【发布时间】:2012-12-26 17:48:34
【问题描述】:

我正在使用blueimp文件上传插件(基础版)来实现多文件上传。我正在尝试实现允许用户删除排队文件以进行上传的功能。我无法弄清楚如何正确访问文件数组。每次在 add 回调中,索引为 0,文件数组长度为 1(它只包含用户单击删除的文件)。我正在为每个排队到 div 的文件添加一个链接,该链接是可点击的,如果被点击,应该会删除该文件。

我的想法是创建一个带有文件索引的删除链接并将其从数组中删除,但由于上述问题,索引永远不会正确。我也尝试过文件名,但“on”回调中的文件名始终是第一个选择上传的文件 - 我必须弄清楚一些闭包范围。

如何以编程方式从上传队列中删除文件?

HTML:

<div id="fileBrowserWrapper">
    <form id="myForm" action="#" method="post" enctype="multipart/form-data">
        <input id="uploadDocBrowse" type="file" name="files[]" multiple/>                                                    
    </form>
</div>
<div id="inputFilesBox"></div>
<div id="uploadFilesBox"></div>

和文件上传JavaScript:

$('#myForm').fileupload({
    url: "/SomeHandler",
    dataType: 'html',
    autoUpload: false,
    singleFileUploads: false,
    replaceFileInput: false,
    add: function (e, data) {
        console.log("Number of files: " + data.files.length);

        $.each(data.files, function (index, file) {                                       
            $('#uploadFilesBox').append("<div class='uploadBox' id='fileDiv_" + file.name + "'><div class='leftEle'><a href='#' id='link_" + index + "' class='removeFile'>Remove</a></div><div class='midEle'>" + file.name + "</div></div>")
            .on('click', { filename: file.name, files: data.files }, function(event) {                            
                var uploadFilesBox = $("#uploadFilesBox");
                var remDiv = $("#fileDiv_" + event.data.filename);
                remDiv.remove();
                event.data.files.splice(0, 1);                              
            }
        });
    });

    data.context = $('#myButton')
    .click(function () {
        data.submit();
    });              
});

【问题讨论】:

  • 我解决了这个问题,请看我原来问题的顶部。
  • 请发布您的解决方案作为答案。这就是未来读者寻找解决方案的地方。将它包含在问题中大多看起来令人困惑。谢谢! :)
  • 我必须等待 8 小时才能回答我自己的问题 :)
  • 公平点。我通常不这样做,但现在是假期,我想发挥一些员工魔法。我发布了答案并将其归功于您。如果我抓住了帖子的错误部分或其他内容,请随时进行编辑。 :)

标签: jquery jquery-plugins file-upload blueimp


【解决方案1】:

适用于我的多个文件 - 它检查所有文件并且当有错误的文件是所有文件中间的一个时不会中断(如.splice().lenght=0 做)。 想法是:进行验证->如果错误:标记文件索引错误->在所有文件之后/上传之前:删除/删除错误$.grep()的索引/文件->一起上传好文件singleFileUploads: false

$(this).fileupload({
        // ...
        singleFileUploads: false,   // << send all together, not single
        // ...
        add: function (e, data) {

            // array with all indexes of files with errors
            var error_uploads_indexes = [];

            // when add file - each file
            $.each(data.files, function(index, file) {

                // array for all errors - in example is only one: size
                var uploadErrors = [];

                // ... validation

                        // check size
                        if(data.files[index]['size'] > 1048576) {
                            uploadErrors.push('Filesize is too big');
                        };
                // ...

                // when errors
                if(uploadErrors.length > 0) {

                    // mark index of error file
                    error_uploads_indexes.push(index);
                    // alert error
                    alert(uploadErrors.join("\n"));

                };

            }); // << each


            // remove indexes (files) with error
            data.files = $.grep( data.files, function( n, i ) {
                return $.inArray(i, error_uploads_indexes) ==-1;
            });


            // if are files to upload
            if(data.files.length){
                // upload by ajax
                var jqXHR = data.submit().done(function (result, textStatus, jqXHR) {
                        //...
                     alert('done!') ;
                        // ...
                });
            } // 

        },
        // ...
    }); // << file_upload

【讨论】:

    【解决方案2】:

    感谢@Furynation。

    我所做的与您的方法相似。对于我选择的每个文件,我都会在表格中添加一行(上传前提交)。我将此行分配给 data.context 以供以后使用。

    见: https://github.com/blueimp/jQuery-File-Upload/issues/3083

    我的代码 sn-p 在添加回调处理程序中:

     $("#upload").click(function () {
                     if (data.files.length > 0) { 
                         data.submit();
                     }
                });
                data.context.find('a').on('click',function (event) {  
                    event.preventDefault();
                    data.context.remove();   
                    data.files.length = 0;   
                });
    

    这将删除表格行并重置数组。

    如果有更清洁的方法,请告诉我。

    【讨论】:

      【解决方案3】:

      我解决了这个问题。这是带有描述的解决方案:

      在对它进行了更多修改后,我找到了我的解决方案。关键是要记住我正在回电。因此,在删除功能的事件处理程序中,我只是将 data.files 数组清零,并且在该处理程序的提交中,我仅在 files 数组的长度大于 0 时提交。我清理了事件处理程序函数,所以眼睛更容易。 HTML 没有改变。

      新的 JavaScript 处理代码:

       $('#myForm').fileupload({
                  url: "/SomeUrl",
                  dataType: 'html',            
                  add: function (e, data) {
                      $.each(data.files, function (index, file) {
                          var newFileDiv = $("<div class='uploadBox' id='fileDiv_" + file.name + "'><div class='leftEle'><a href='#' id='link_" + index + "' class='removeFile'>Remove</a></div><div class='midEle'>" + file.name + "</div></div>");
                          $('#uploadFilesBox').append(newFileDiv);
      
                          newFileDiv.find('a').on('click', { filename: file.name, files: data.files }, function (event) {                        
                              event.preventDefault();
                              var uploadFilesBox = $("#uploadFilesBox");
                              var remDiv = $(document.getElementById("fileDiv_" + event.data.filename));
                              remDiv.remove();                        
                              data.files.length = 0;    //zero out the files array                                     
                          });
      
                          data.context = newFileDiv;
                      });
      
                      $('#myButton')
                          .click(function () {
                              if (data.files.length > 0) {     //only submit if we have something to upload
                                  data.submit();
                              }                                                    
                          });
                  }
      });
      

      【讨论】:

        猜你喜欢
        • 2016-10-31
        • 2016-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-24
        • 2018-07-16
        • 2015-03-17
        • 2016-05-26
        相关资源
        最近更新 更多