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

前端 未结 15 907
深忆病人
深忆病人 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 08:57

    Just an example of event handler for Add event. Assumes that singleFileUploads option is enabled (which is the default). Read more jQuery File Upload documentation how to bound with add/fileuploadadd event. Inside loop you can use both vars this or file. An example of getting size property: this['size'] or file.size.

        /**
         * Handles Add event
         */
        base.eventAdd = function(e, data) {
    
            var errs = [];
            var acceptFileTypes = /(\.|\/)(gif|jpe?g|png)$/i;
            var maxFileSize = 5000000;
    
            // Validate file
            $.each(data.files, function(index, file) {
                if (file.type.length && !acceptFileTypes.test(file.type)) {
                    errs.push('Selected file "' + file.name + '" is not alloawed. Invalid file type.');
                }
                if (this['size'] > maxFileSize) {
                    errs.push('Selected file "' + file.name + '" is too big, ' + parseInt(file.size / 1024 / 1024) + 'M.. File should be smaller than ' + parseInt(maxFileSize / 1024 / 1024) + 'M.');
                }
            });
    
            // Output errors or submit data
            if (errs.length > 0) {
                alert('An error occured. ' + errs.join(" "));
            } else {
                data.submit();
            }
        };
    

提交回复
热议问题