亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在上傳前預覽圖像,獲取文件大小、圖像高度和寬度?

如何在上傳前預覽圖像,獲取文件大小、圖像高度和寬度?

如何在上傳前預覽圖像,獲取文件大小、圖像高度和寬度?在上傳到我的網站之前,如何使用jQuery或JavaScript獲取文件大小、圖像高度和寬度?
查看完整描述

3 回答

?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

如果您可以使用jQuery驗證插件,您可以這樣做:

HTML:

<input type="file" name="photo" id="photoInput" />

JavaScript:

$.validator.addMethod('imagedim', function(value, element, param) {
  var _URL = window.URL;
        var  img;
        if ((element = this.files[0])) {
            img = new Image();
            img.onload = function () {
                console.log("Width:" + this.width + "   Height: " + this.height);
                //this will give you image width and height and you can easily validate here....

                return this.width >= param            };
            img.src = _URL.createObjectURL(element);
        }});

該函數作為ab onload函數傳遞。

代碼取自這里


查看完整回答
反對 回復 2019-06-24
?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

演示

不確定這是否是你想要的,只是簡單的例子:

var input = document.getElementById('input');input.addEventListener("change", function() {
    var file  = this.files[0];
    var img = new Image();

    img.onload = function() {
        var sizes = {
            width:this.width,
            height: this.height        };
        URL.revokeObjectURL(this.src);

        console.log('onload: sizes', sizes);
        console.log('onload: this', this);
    }

    var objectURL = URL.createObjectURL(file);

    console.log('change: file', file);
    console.log('change: objectURL', objectURL);
    img.src = objectURL;});


查看完整回答
反對 回復 2019-06-24
  • 3 回答
  • 0 關注
  • 1086 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號