4

On a webpage, once images have been downloaded and rendered, I need to determine an image's file size (kb) within the browser context (so I could display that info on the page, just below the image)

2
  • Maybe new XmlHttpRequest().getResponseHeader('Content-Length') is useful. Commented Sep 10, 2014 at 20:04
  • Asked and answered on here already. Please search before asking. stackoverflow.com/questions/1310378/… Commented Sep 10, 2014 at 20:05

1 Answer 1

3

The easiest way is probably with a HEAD request returning the Content-Length:

function fileSize(img, func) {
  var xhr = new XMLHttpRequest();
  xhr.open('HEAD', img.src, true);
  xhr.onreadystatechange = function() {
    if(xhr.readyState == 4 && xhr.status == 200) {
      func(xhr.getResponseHeader('Content-Length')) 
    }
  }
  xhr.send()
}

Usage

fileSize(imgNode, function(size) {
  // ...
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.