0

I have several images on a page, which all have the class

.imgToPrint

I want to loop through the list and get the file size for each using the file API.

I'm trying like this

$(".imgToPrint").each(function(){               
 console.log( $(this).files[0].size );
});

but I get the error Uncaught TypeError: Cannot read property '0' of undefined

how can I get the file size of each image in this class?

Julian
  • 33,915
  • 22
  • 119
  • 174
Shawn
  • 3,031
  • 4
  • 26
  • 53

1 Answers1

0

the answer was found here and I implemented it this way

$(".imgToPrint").each(function(e){   
        var srcx=$(this).attr("src");           
        console.log(srcx);
        var xhr = new XMLHttpRequest();
        xhr.open('HEAD', srcx, true);
        xhr.onreadystatechange = function(){
          if ( xhr.readyState == 4 ) {
            if ( xhr.status == 200 ) {
              console.log('Size in bytes: ' + xhr.getResponseHeader('Content-Length'));
            } else {
              console.log('ERROR');
            }
          }
        };
        xhr.send(null);
}); 
Shawn
  • 3,031
  • 4
  • 26
  • 53