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

前端 未结 15 917
深忆病人
深忆病人 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:04

    As suggested in an earlier answer, we need to include two additional files - jquery.fileupload-process.js and then jquery.fileupload-validate.js However as I need to perform some additional ajax calls while adding a file, I am subscribing to the fileuploadadd event to perform those calls. Regarding such a usage the author of this plugin suggested the following

    Please have a look here: https://github.com/blueimp/jQuery-File-Upload/wiki/Options#wiki-callback-options

    Adding additional event listeners via bind (or on method with jQuery 1.7+) method is the preferred option to preserve callback settings by the jQuery File Upload UI version.

    Alternatively, you can also simply start the processing in your own callback, like this: https://github.com/blueimp/jQuery-File-Upload/blob/master/js/jquery.fileupload-process.js#L50

    Using the combination of the two suggested options, the following code works perfectly for me

    $fileInput.fileupload({
        url: 'upload_url',
        type: 'POST',
        dataType: 'json',
        autoUpload: false,
        disableValidation: false,
        maxFileSize: 1024 * 1024,
        messages: {
            maxFileSize: 'File exceeds maximum allowed size of 1MB',
        }
    });
    
    $fileInput.on('fileuploadadd', function(evt, data) {
        var $this = $(this);
        var validation = data.process(function () {
            return $this.fileupload('process', data);
        });
    
        validation.done(function() {
            makeAjaxCall('some_other_url', { fileName: data.files[0].name, fileSizeInBytes: data.files[0].size })
                .done(function(resp) {
                    data.formData = data.formData || {};
                    data.formData.someData = resp.SomeData;
                    data.submit();
            });
        });
        validation.fail(function(data) {
            console.log('Upload error: ' + data.files[0].error);
        });
    });
    

提交回复
热议问题