最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dropzone.js defining MinWidth and MinHeight for image upload - Stack Overflow

programmeradmin0浏览0评论

Is there a way to define a minWidth and minHeight for images to be uploaded?
And in the case for the image to have dimensions lower than the minimum defined dimensions it should display an error like the default for images not corresponding the permitted file type but saying something like: "you can't upload images with width lower than 500px".

How could I do this?

Is there a way to define a minWidth and minHeight for images to be uploaded?
And in the case for the image to have dimensions lower than the minimum defined dimensions it should display an error like the default for images not corresponding the permitted file type but saying something like: "you can't upload images with width lower than 500px".

How could I do this?

Share Improve this question asked Dec 9, 2014 at 2:17 CIRCLECIRCLE 4,8795 gold badges42 silver badges58 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

From their page, when creating the dropzone you can specify an accept function which validates the dropped file:

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

As for getting the height and width before upload, AFAIK the best way at the moment is to create a hidden img tag. See this fiddle for example http://jsfiddle/superscript18/nry4h/

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 puter.
        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(); }
}
}
发布评论

评论列表(0)

  1. 暂无评论