javascript/jquery size and dimension of uploaded image file

前端 未结 3 863
遇见更好的自我
遇见更好的自我 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:29

    All you have to do to use the two codes is to combine them in the displayPreview function. You can create the image object that will append to the preview and find it's size, width, and height all in the same function.

    var _URL = window.URL || window.webkitURL;
    function displayPreview(files) {
       var file = files[0];//get file   
       var img = new Image();
       var sizeKB = file.size / 1024;
       img.onload = function() {
          $('#preview').append(img);
          alert("Size: " + sizeKB + "KB\nWidth: " + img.width + "\nHeight: " + img.height);
       }
       img.src = _URL.createObjectURL(file);
    }
    

提交回复
热议问题