How to find image data size using javascript? -
on webpage, once images have been downloaded , rendered, need determine image's file size (kb) within browser context (so display info on page, below image)
the easiest way head
request returning 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) { // ... })
Comments
Post a Comment