8

I'd like to be able to get the file size on an image on a webpage.

So let's say I have an image on the page (that has loaded) like this:

How do I call a function in Javascript (or, even better, jquery) to get the file size (not the dimensions) of the image?

It's important to note that I'm not using any inputs or having users upload the image, there's lots of SO answers on getting image sizes from browse buttons with the file API.

All I want to do is get the file size of any arbitrary image on the page based of it's id and src url.

Edit: I'm dealing with a keep-alive connection for some images so the Content-Length headers are not available.

Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
CafeHey
  • 5,699
  • 19
  • 82
  • 145
  • 2
    possible duplicate of [Determining image file size + dimensions via Javascript?](http://stackoverflow.com/questions/1310378/determining-image-file-size-dimensions-via-javascript) – Joren Jan 03 '14 at 15:53
  • Only way I know how to do that is to use AJAX to call the file and get the content-length header and process it. – Eli Gassert Jan 03 '14 at 15:53
  • @Popnoodles, that's not what the OP has asked. In fact, the question even clarifies "_(not the dimensions)_." – rgthree Jan 03 '14 at 16:01
  • an ajax head request's content-length header will give you the info you need. – dandavis Jan 03 '14 at 16:09
  • @dandavis I'm dealing with a keep-alive connection for some images so that's not possible in this case. – CafeHey Jan 03 '14 at 16:25
  • Look there buddy:
    http://stackoverflow.com/questions/1310378/determining-image-file-size-dimensions-via-javascript/1310399#1310399
    This works to you? :)
    – Rafael Soufraz Jan 03 '14 at 16:34
  • @rafaelsoufraz He **explictly** said **he wants size in bytes** (he don't need the DIMENSION of image). And in bold he also edited and written that **he couldn't use Content-Length** due to keep-alive connections. – Denys Vitali Jan 03 '14 at 16:43
  • 1
    if you can't do the ajax HEAD, you have to do a binary ajax and look at the size of the response. – dandavis Jan 03 '14 at 16:44
  • @rafaelsoufraz the post you linked deals primarily with the dimensions, and the API used there to determine the file size in bytes is no longer supported – S Grimminck Jan 03 '14 at 16:45

4 Answers4

3

You can't directly get the file size (or any data from it).

The only way is a bit dirty, because you have to do a XMLHTTPRequest (and it probably won't work with externals images, according to the "Cross Origin Resource Sharing"). But with the browser's cache, it should not cause another HTTP request.

var xhr = new XMLHttpRequest();
xhr.open("GET", "foo.png", true);
xhr.responseType = "arraybuffer";
xhr.onreadystatechange = function() {
    if(this.readyState == this.DONE) {
        alert("Image size = " + this.response.byteLength + " bytes.");
    }
};
xhr.send(null);
Sebastien C.
  • 4,649
  • 1
  • 21
  • 32
1

Here is a React version that worked for me, posting it in the event that it could save some time.

Form field

<input type="file" 
   name="file"  
   id="file" 
   multiple="multiple" 
   onChange={onChangeHandler} 
   className="fileUp" />

JSX

const onChangeHandler = event => {
    console.log(event.target.files[0].size);
}
Michael Nelles
  • 5,426
  • 8
  • 41
  • 57
1

Old question but here is my take: I had the same issue but didn't want te rely on buffering to prevent my app from downloading the images twice (as the accepted answer does). What I ended up doing was fetch the file as a blob, read the file size, and use the blob as image source from there on. this was easily done with the URL.createObjectURL() function like so:

let img = new Image()
img.crossOrigin = "Anonymous"; // Helps with some cross-origin issues

fetch('foo.png')
.then(response => response.blob())
.then(blob => { img.src = URL.createObjectURL(blob); })

This looks a lot cleaner and perhaps it can still be useful for some.

Sam
  • 300
  • 3
  • 10
-1

Assuming you could use HTML5

  1. Create a canvas
  2. Put image in there
  3. Grab the image data with .toDataURLHD()
  4. Grab the base64 encoded string
  5. Use some mad calculation to get the image size in bytes.

Have fun!

Maybe it's a lot easier to use a server side script, so it won't be HTML5-dependant

Community
  • 1
  • 1
Denys Vitali
  • 530
  • 6
  • 19
  • 1
    i don't think this would work to get filesize on compressed images: a 100,000 pixel jpeg could take 1kb or 100kb depending on compression; – dandavis Jan 03 '14 at 16:43
  • @dandavis He actually wants the image file size in bytes, not actually the dimensions (in pixels). Read better the question! --Edit-- Wait, maybe I got confused – Denys Vitali Jan 03 '14 at 16:46
  • right, but recompressing/dumping the image data won't tell you the size of the original file... – dandavis Jan 03 '14 at 16:47
  • Where do I recompress it in my task list? – Denys Vitali Jan 03 '14 at 16:48
  • toDataURLHD() exports png or jpeg: either one is almost certain to be a different final filesize than the orig, unless by pure coincidence. also, atob(dataurl.split(",")[1]).length is a less janky way to get the actual file size of a dataURL. if in doubt, go ahead and try to export the same image you imported and arrive at the exact same file size. i dare you ;) – dandavis Jan 03 '14 at 16:57
  • @dandavis Do you have another chance? I'm run out of possibilities D: – Denys Vitali Jan 03 '14 at 17:37