6

I am trying below code to get the image size in KB/MB/GB (not width and height) from live URL. But it's not working.

var xhr = new XMLHttpRequest();

xhr.open('HEAD', 'http://www.2015autoblog.com/wp-content/uploads/2014/09/2015-Toyota-Land-Cruiser-Front.jpg', true);

debugger

alert(xhr.status);

if ( xhr.status == 200 ) {
    alert('Size in bytes: ' + xhr.getResponseHeader('Content-Length'));
} else {
    alert('ERROR');
}
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Prashant Sarvaiya
  • 364
  • 1
  • 3
  • 14

1 Answers1

16

Try this code it is working

var tmpImg = new Image();
tmpImg.src="https://www.google.com/intl/en_com/images/srpr/logo3w.png"; //or  document.images[i].src;
$(tmpImg).one('load',function(){
  orgWidth = tmpImg.width;
  orgHeight = tmpImg.height;
  alert(orgWidth+"x"+orgHeight);
});

The above code will gets the image dimension.

For the file size: No, there is no way you can get the image size using jQuery or pure JavaScript. The only way is to get it from server side using ajax.

You can get the image url send it to a service or server side page and have the page or service return the image size.

user46487
  • 644
  • 6
  • 8