获取file上传图片的宽高(原图片的宽高)

//获取input图片宽高
function getImageWidthAndHeight(id, callback) {
    var _URL = window.URL || window.webkitURL;
    $("#" + id).change(function (e) {
        var file, img;
        if ((file = this.files[0])) {
            img = new Image();
            img.onload = function () {
                callback && callback({"width": this.width, "height": this.height});
            };
            img.src = _URL.createObjectURL(file);
        }
    });
}
这里使用了一个回调方法。
在jQuery中使用:

(function () {
   getImageWidthAndHeight('image_file', function (obj) {
     if (obj.width != 843 || obj.height != 1038) {
        $.messager.alert('操作提示', result.data.msg, '弹窗图片宽高必须是843*1038px');
     }
   });
})(jQuery)

 

posted @ 2017-02-22 10:04  鲍攻城  阅读(3378)  评论(0编辑  收藏  举报