Get file size, image width and height before upload

前端 未结 7 1246
自闭症患者
自闭症患者 2020-11-22 03:50

How can I get the file size, image height and width before upload to my website, with jQuery or JavaScript?

7条回答
  •  心在旅途
    2020-11-22 04:40

    Demo

    Not sure if it is what you want, but just simple example:

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

提交回复
热议问题