提示上传图像的宽度和高度

时间:2013-01-16 07:23:05

标签: javascript jquery asp.net file-upload

我正在寻找一些方法来使用asp文件上传控件来提示正在上传的图像的宽度和高度。以下是我用于提示消息文件大小的代码。我只想添加使用该上传控件上传的图像的宽度和高度。请注意我不能在img.clientWidth这里使用javascript DOM,因为没有这样的html对象可以调用它。

这是JS代码

$(document).ready(function () {
    $("#<%= fupNewImage.ClientID %>").bind("change", function () {
        var fSize = ((this.files[0].size) / 1024).toFixed(2);
        if (fSize <= 200) {
            $("#fupSize").html('Good file size: ' + fSize + " kb  Width=? and Height=?" ); // ? denotes the actual value to be put.
        }
        //---------More cases
    });
});

1 个答案:

答案 0 :(得分:0)

解决!!

感谢@Esailija

经过大量的搜索,我终于找到了锻炼@ REF https://stackoverflow.com/a/8904008/1584140

var _URL = window.URL || window.webkitURL;
    $("#<%= fupNewImage.ClientID %>").bind("change", function () {
        //================Changed Code===================
        var file, img;
        if ((file = this.files[0])) {
            img = new Image();
            img.onload = function () {
                var fSize = ((file.size) / 1024).toFixed(2);
                var WH = " Widht: " + this.width + "px & Height: " + this.height + "px";

                if (fSize <= 200) {
                    $("#fupSize").html('Good file size: ' + fSize + " kb" + WH);
                }
            };
            img.onerror = function () {
                alert("not a valid file: " + file.type);
            };
            img.src = _URL.createObjectURL(file);
        }
        //====================================
    });
});