How to get byte size of image file using node.js

I need to get the size of image files that are on a web page using node.js. For this, I used some node modules, but it gives the size of all other resources, such as js file, html size, etc. How to get the request size (request for an image file) ?, How to find the download time of an image file?

+4
source share
2 answers

I assume that you are trying to get the image file size via http, not the image sizes. (Please correct me if I am wrong)

You can simply send a HEAD request to the resource and see the Content-Length header:

var http = require('http'); var req = http.request({ host: 'www.gravatar.com', port: 80, path: '/avatar/b9da20552a71c4d3c4c6dc2d55418cf2', method: 'HEAD' }, function(res) { console.log("Size: " + res.headers['content-length']); } ).end(); 
+5
source

This post seems to have been useful to another user regarding the method of capturing a byte file. You can probably ignore the details about getting the sizes.

Determining image file size + sizes using Javascript?

As with the other problem, search time, this may work:

 var obj = new Image(); obj.src = "imglocation.png"; for (i = 1; i != -1; i++) { if (obj.complete) { /*do stuff*/ } } 

It might work. I am not a JavaScripter, so I am not configured for it. And although this is not Node.js, I'm sure you can change the code to use it easily?

0
source

Source: https://habr.com/ru/post/1400975/


All Articles