Dropzone.js定义MinWidth和MinHeight用于图像上传

时间:2014-12-09 02:17:55

标签: javascript dropzone.js

有没有办法为要上传的图像定义minWidth和minHeight? 在图像尺寸低于最小定义尺寸的情况下,它应该显示错误,如图像的默认值不对应于允许的文件类型,但说出类似的内容:"您无法上传图像宽度低于500px"。
enter image description here
我怎么能这样做?

2 个答案:

答案 0 :(得分:3)

在他们的页面中,创建dropzone时,您可以指定一个accept函数来验证删除的文件:

Dropzone.options.myAwesomeDropzone = {
  paramName: "file", // The name that will be used to transfer the file
  maxFilesize: 2, // MB
  accept: function(file, done) {
    if (/* Get dimensions and check */) {
      done("Invalid dimensions!");
    }
    else { done(); }
  }
};

至于在上传之前获取高度和宽度,AFAIK目前最好的方法是创建一个隐藏的img标签。请参阅此小提琴,例如http://jsfiddle.net/superscript18/nry4h/

答案 1 :(得分:0)

Dropzone.options.myAwesomeDropzone = {
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 2, // MB
accept: function(file, done) {
// FileReader() asynchronously reads the contents of files (or raw data buffers) stored on the user's computer.
        var reader = new FileReader();
        reader.onload = (function(entry) {
          // The Image() constructor creates a new HTMLImageElement instance.
          var image = new Image(); 
          image.src = entry.target.result;
          image.onload = function() {

            console.log(this.width);
            console.log(this.height);

          };
        });

        reader.readAsDataURL(file);
if (/* Get dimensions and check */) {
  done("Invalid dimensions!");
}
else { done(); }
}
}