How to get dimensions of the image in Fine Uploader before submitted to server and validate it?

若如初见. 提交于 2019-12-06 13:58:26

Answering my own question after searching for a while.

I borrowed the logic from source code of Fine Uploader where the dimensions of the image are determined. The key point is to return a promise in the onSubmit handler.

Note: my_custom_validation is a custom method outside of the scope of this answer. You should have your own implementation for your needs.

onSubmit: function(id, name){
    var promise = new qq.Promise();
    var file = this.getFile(id);
    var image = new Image();

    var url = window.URL && window.URL.createObjectURL ? window.URL :
      window.webkitURL && window.webkitURL.createObjectURL ? window.webkitURL :
      null;

    image.onload = function(){
        if(my_custom_validation(this.width, this.height)){
            promise.success();    
        } else {
            promise.failure();
        }

    };

    image.src = url.createObjectURL(file);

    return promise;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!