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

前端 未结 15 903
深忆病人
深忆病人 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:00
    • You can also use the file extension to check for the filetype.
    • More simpler way would be to do something as given below inside add :

      add : function (e,data){
         var extension = data.originalFiles[0].name.substr( 
         (data.originalFiles[0].name.lastIndexOf('.') +1) );
                  switch(extension){
                      case 'csv':
                      case 'xls':
                      case 'xlsx':
                          data.url = <Your URL>; 
                          data.submit();
                      break;
                      default:
                          alert("File type not accepted");
                      break;
                  }
        }
      
    0 讨论(0)
  • 2020-12-07 09:01

    This works for me in firefox

    $('#fileupload').fileupload({
    
        dataType: 'json',
        //acceptFileTypes: /(\.|\/)(xml|pdf)$/i,
        //maxFileSize: 15000000,
    
        add: function (e, data) {
            var uploadErrors = [];
    
            var acceptFileTypes = /\/(pdf|xml)$/i;
            if(data.originalFiles[0]['type'].length && !acceptFileTypes.test(data.originalFiles[0]['type'])) {
                uploadErrors.push('File type not accepted');
            }
    
            console.log(data.originalFiles[0]['size']) ;
            if (data.originalFiles[0]['size'] > 5000000) {
                uploadErrors.push('Filesize too big');
            }
            if(uploadErrors.length > 0) {
                alert(uploadErrors.join("\n"));
            } else {
                data.context = $('<p/>').text('Uploading...').appendTo(document.body);
                data.submit();
            }
    
        },
        done: function (e, data) {
            data.context.text('Success!.');
        }
    });
    
    0 讨论(0)
  • 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);
        });
    });
    
    0 讨论(0)
  • 2020-12-07 09:04
    .fileupload({
        add: function (e, data) { 
            var attachmentValue = 3 * 1000 * 1024;
            var totalNoOfFiles = data.originalFiles.length;
            for (i = 0; i < data.originalFiles.length; i++) {
                if (data.originalFiles[i]['size'] > attachmentValue) {
                    $attachmentsList.find('.uploading').remove();
                    $attachmentMessage.append("<li>" + 'Uploaded bytes exceeded the file size' + "</li>");
                    $attachmentMessage.show().fadeOut(10000, function () {
                        $attachmentMessage.html('');
                    });
                    data.originalFiles.splice(i, 1);
                }
            }
            if (data.files[0]) {
                $attachmentsList
               .prepend('<li class="uploading" class="clearfix loading-content">' + data.files[0].name + '</li>');
            }
            data.submit();                    
        }
    
    0 讨论(0)
  • 2020-12-07 09:06

    This worked for me in chrome, jquery.fileupload.js version is 5.42.3

         add: function(e, data) {
            var uploadErrors = [];
            var ext = data.originalFiles[0].name.split('.').pop().toLowerCase();
            if($.inArray(ext, ['odt','docx']) == -1) {
                uploadErrors.push('Not an accepted file type');
            }
            if(data.originalFiles[0].size > (2*1024*1024)) {//2 MB
                uploadErrors.push('Filesize is too big');
            }
            if(uploadErrors.length > 0) {
                alert(uploadErrors.join("\n"));
            } else {
                data.submit();
            }
        },
    
    0 讨论(0)
  • 2020-12-07 09:07

    You should include jquery.fileupload-process.js and jquery.fileupload-validate.js to make it work.

    0 讨论(0)
提交回复
热议问题