Delete files programmatically with jquery fileupload basic

前端 未结 3 1849
攒了一身酷
攒了一身酷 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: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
    

提交回复
热议问题