I have some images in url(s). I can get file properties and properties include width and height of image as well. I want to get the size in bytes.
I am trying to get size using fs module as shown below, but it is not working with url, though it works with file path in local folder.
var stats = fs.statSync(url);
var fileSizeInBytes = stats["size"]
To get the size of a file in Node. js, you can use the stat() method provided by the built-in fs module. This method works asynchronously and lists the statistics of a file at the given path. The size of the file is returned in bytes.
To check the buffer window, multiply the bit rate (bits per second) by the buffer window (in seconds) and divide by 1000 to get the size, in bits, of the buffer for the stream.
The fs. statSync() method is used to synchronously return information about the given file path.
In Node. js, you can use the fs. unlink() method provided by the built-in fs module to delete a file from the local file system.
You have to use request
, or http
. You can get the file size by sending a HEAD
request and inspect content-length
field (it will not work on every server):
With curl
:
curl -I "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/core.js"
You get the response:
HTTP/1.1 200 OK
Date: Tue, 03 Apr 2018 14:30:16 GMT
Content-Type: application/javascript; charset=utf-8
Content-Length: 9068
Connection: keep-alive
Last-Modified: Wed, 28 Feb 2018 04:16:30 GMT
ETag: "5a962d1e-236c"
Expires: Sun, 24 Mar 2019 14:30:16 GMT
Cache-Control: public, max-age=30672000
Access-Control-Allow-Origin: *
CF-Cache-Status: HIT
Accept-Ranges: bytes
Strict-Transport-Security: max-age=15780000; includeSubDomains
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 405c3b6e1911a8db-CDG
With request
module :
var request = require("request");
request({
url: "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/core.js",
method: "HEAD"
}, function(err, response, body) {
console.log(response.headers);
process.exit(0);
});
You get the response:
{
date: 'Tue, 03 Apr 2018 14:29:32 GMT',
'content-type': 'application/javascript; charset=utf-8',
'content-length': '9068',
connection: 'close',
'last-modified': 'Wed, 28 Feb 2018 04:16:30 GMT',
etag: '"5a962d1e-236c"',
expires: 'Sun, 24 Mar 2019 14:29:32 GMT',
'cache-control': 'public, max-age=30672000',
'access-control-allow-origin': '*',
'cf-cache-status': 'HIT',
'accept-ranges': 'bytes',
'strict-transport-security': 'max-age=15780000; includeSubDomains',
'expect-ct': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"',
server: 'cloudflare',
'cf-ray': '405c3a5cba7a68ba-CDG'
}
Get file size from URL without downloading it. 0 dependencies.
Returns Promise with 'B', 'KB', 'MB', 'GB', 'TB' on success.
npm i file_size_url
import file_size_url from 'file_size_url';
file_size_url("https://server10.mp3quran.net/bader/Rewayat-Hafs-A-n-Assem/001.mp3")
.then(console.log) // 968.27 KB
.catch(console.error);
OR
import file_size_url from 'file_size_url';
let size = await file_size_url("https://serverjyy10.mp3quran.net/bader/Rewayat-Hafs-A-n-Assem/0001.mp3")
.catch((error) => console.log(error))
console.log(size) // 968.27 KB
Example for lop
let array = [
'https://server10.mp3quran.net/bader/Rewayat-Hafs-A-n-Assem/001.mp3',
'https://server10.mp3quran.net/bader/Rewayat-Hafs-A-n-Assem/002.mp3',
'https://server10.mp3quran.net/bader/Rewayat-Hafs-A-n-Assem/003.mp3',
'https://server10.mp3quran.net/bader/Rewayat-Hafs-A-n-Assem/055.mp3',
'https://server10.mp3quran.net/bader/Rewayat-Hafs-A-n-Assem/110.mp3',
]
for (let index = 0; index < array.length; index++) {
try {
let fies_size = await file_size_url(array[index])
if (fies_size.toString().split('.')[0] <= 95 && fies_size.toString().split(' ')[1] === 'MB') {
console.log(fies_size + ' || <= 95 MB');
}
else if (fies_size.toString().split('.')[0] > 95 && fies_size.toString().split(' ')[1] === 'MB') {
console.log(fies_size + ' || > 95 MB');
}
else if (fies_size.toString().split(' ')[1] === 'KB') {
console.log(fies_size + ' || KB');
}
} catch (error) {
console.log(error);
}
}
/* output
968.27 KB || KB
170.58 MB || > 95 MB
95.77 MB || <= 95 MB
12.21 MB || <= 95 MB
711.92 KB || KB
*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With