maxFileSize and acceptFileTypes in blueimp file upload plugin do not work. Why?

前端 未结 15 937
深忆病人
深忆病人 2020-12-07 08:34

I\'m using Blueimp jQuery file upload plugin for upload files.

I had no problem in uploading but the option maxFileSize and acceptFileTypes

15条回答
  •  渐次进展
    2020-12-07 09:08

    Checked/Valid example for:

    • multiple file inputs
    • for one or MULTIPLE FILES upload - $.grep() to remove files from array with errors
    • image and audio format
    • dynamic file types from string by new RegExp()

    Notice: acceptFileTypes.test() - check mime types, for adio file like .mp3 it will be audio/mpeg - not only extenstion. For all blueimp options: https://github.com/blueimp/jQuery-File-Upload/wiki/Options

    $('input[type="file"]').each(function(i){
    
        // .form_files is my div/section of form for input file and progressbar
        var $progressbar = $(this).parents('.form_files:first').find('.progress-bar:first');
    
        var $image_format = 'jpg|jpeg|jpe|png|gif';
        var $audio_format = 'mp3|mpeg';
        var $all_formats = $image_format + '|' + $audio_format;
    
        var $image_size = 2;
        var $audio_size = 10;
        var mb = 1048576;
    
        $(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
                    var uploadErrors = [];
    
    
                    // validate all formats first
                    if($all_formats){
    
                        // check all formats first - before size
                        var acceptFileTypes = "(\.|\/)(" + $all_formats + ")$";
                        acceptFileTypes = new RegExp(acceptFileTypes, "i");
    
                        // when wrong format
                        if(data.files[index]['type'].length && !acceptFileTypes.test(data.files[index]['type'])) {
                            uploadErrors.push('Not an accepted file type');
    
                        }else{
    
                            // default size is image_size
                            var $my_size = $image_size;
    
                                // check audio format
                                var acceptFileTypes = "(\.|\/)(" + $audio_format + ")$";
                                acceptFileTypes = new RegExp(acceptFileTypes, "i");
    
                                // alert(data.files[index]['type']);
                                // alert(acceptFileTypes.test('audio/mpeg'));
    
                                // if is audio then size is audio_size
                                if(data.files[index]['type'].length && acceptFileTypes.test(data.files[index]['type'])) {
                                    $my_size = $audio_size;
                                }
    
                            // check size
                            if(data.files[index]['size'] > $my_size * mb) {
                                uploadErrors.push('Filesize is too big');
                            };
                        };
    
                    }; // << all_formats
    
                    // when errors
                    if(uploadErrors.length > 0) {
                        //  alert(uploadErrors.join("\n"));
    
                        // 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!') ;
                            // ...
                    });
                } // 
    
            }, // << add
            progressall: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                $progressbar.css(
                    'width',
                    progress + '%'
                    );
            }
        }); // << file_upload
    
        //          
    }); // << each input file
    

提交回复
热议问题