Dropzone检查图像尺寸和文件大小

hah*_*aha 6 jquery dropzone.js

我正在使用Dropzonejs插件。上传文件时,我想检查图像尺寸(宽度和高度)以及文件大小。我设法检查了尺寸和文件大小,但是当我将两者结合使用时,效果并不理想。

var maxImageWidth = 2500, 
    maxImageHeight = 2500;

Dropzone.options.formUserEdit = {
    maxFilesize: 2,
    acceptedFiles: 'image/*',
    success : function(file, Response){
      var obj = JSON.parse(Response);
      $('#form-user-edit').append('<input type="hidden" name="userprofile" data-ori="'+file.name+'" value="'+obj.filename+'" />');

      $('.error-msg').remove();
    },
    init: function() {
        this.on("thumbnail", function(file) {
            if (file.width > maxImageWidth || file.height > maxImageHeight) {
                file.rejectDimensions();
            else {
                file.acceptDimensions();
            }
        })
    },
    accept: function(file, done) {
        file.rejectDimensions = function() { 
            done("Please make sure the image width and height are not larger than 2500px."); 
        };
        file.acceptDimensions = done;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果:

  • 上传大尺寸图片:接受该功能

  • 上传小尺寸图片,但大小超过2MB:它将返回file.acceptDimensions is not a function或不会成功

小智 7

万一有人仍然需要答案。

init: function() {
    this.on("thumbnail", function(file) {
        if (file.width > maxImageWidth || file.height > maxImageHeight) {
            file.rejectDimensions();
        else {
            if(file.size < 1024*1024*2/*2MB*/)
            {
                file.acceptDimensions();
            }
        }
    })
},
Run Code Online (Sandbox Code Playgroud)