2016-08-03 98 views
3

我正在使用Dropzonejs插件。我想檢查圖像尺寸(寬度和高度)以及文件上傳時的文件大小。我設法檢查尺寸和文件大小,但是當我將它們組合在一起時,效果不佳。Dropzone檢查圖像尺寸和文件大小

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; 
    } 
} 

如果:

  • 上傳大尺寸圖片:接受功能

  • 上傳小尺寸的圖像,但超過2MB:它將返回file.acceptDimensions is not a function還是不會去成功

回答

0

如果有人仍然需要這個答案。

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(); 
      } 
     } 
    }) 
},