javascript/jquery size and dimension of uploaded image file

前端 未结 3 861
遇见更好的自我
遇见更好的自我 2021-02-04 13:30

I need to display the image size in kilobyte and the dimension (height, width) using javascript/jquery. I came across few similar posts, but none could help me. I have two set o

3条回答
  •  暖寄归人
    2021-02-04 14:03

    You can try like this

    HTML

    
    
    

    JQUERY

    var _URL = window.URL || window.webkitURL;
    
    function displayPreview(files) {
        var img = new Image(),
            fileSize = Math.round(files.size / 1024);
    
        img.onload = function () {
            var width = this.width,
                height = this.height,
                imgsrc = this.src;
    
            doSomething(fileSize, width, height, imgsrc); //call function
    
        };
        img.src = _URL.createObjectURL(files);
    }
    
    // Do what you want in this function
    function doSomething(size, width, height, imgsrc) {
        $('#preview').append('');
        alert("Size=" + size);
        alert("Width=" + width + " height=" + height);
    
    
    }
    

    Both methods

    Jsfiddle http://jsfiddle.net/code_snips/w4y75/ Jsfiddle http://jsfiddle.net/code_snips/w4y75/1/

提交回复
热议问题