Delete files programmatically with jquery fileupload basic

前端 未结 3 1803
攒了一身酷
攒了一身酷 2021-02-05 10:11

I\'m using the blueimp file upload plugin (the basic version) to implement multifile upload. I am trying to implement functionality to allow the user to remove queued files for

相关标签:
3条回答
  • 2021-02-05 10:26

    I solved this. Here is the solution with description:

    I found my solution after tinkering with it some more. The key was remembering that I was in a call back. So in the event handler for the remove functionality, I just zeroed out the data.files array, and in the submit for that handler, I only submit if the files array has a length greater than 0. I cleaned up the event handler function so it's easier on the eyes. HTML is unchanged.

    New JavaScript handling code:

     $('#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();
                            }                                                    
                        });
                }
    });
    
    0 讨论(0)
  • 2021-02-05 10:26

    Thanks for that @Furynation.

    What I did was similar to your approach. For every file I select I add a row to a table(pre upload submission). This row I assign to the data.context to use for later use.

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

    My code snippet is in the add callback handler:

     $("#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;   
                });
    

    This removes the table row and resets the array.

    If there is a cleaner way, please let me know.

    0 讨论(0)
  • 2021-02-05 10:30

    Works for me for multiple files - it checks all files and doesn't break when file with error is one in the middle of all files (like .splice() or .lenght=0 do). Idea is: do validation -> if error: mark index of file with error -> after all files/before upload: remove/delete wrong indexes/files by $.grep() -> upload good files together 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
    
    0 讨论(0)
提交回复
热议问题