[βœ… Solution] Getting File Size

Hello everyone

Is there a way to get the file size of an existing file? Right now, the file object only exposes filename, URL, and ID, but I don’t see any field for file size.

Has anyone found a workaround or method to retrieve the file size programmatically?

Hi @meetmaykel

The simple answer as far as i know is not easily however it is possible with a little bit of jiggery pokery:

const { headers: h } = await http.get(document_field_file_file_url,{
    headers:{
        "Range": "bytes=0-0"
    }
})

const b = jsonata('`content-range`').evaluate(h);
console.warn(b);
const fSize = (parseInt(b.split('/')[1],10)/1024).toFixed(1);
console.info(`File Size: ${fSize}kb`);

we can get the first byte of the file and then extract the total fie size from the headers, this is made more complex by the fact that the header name has a - in it so you have to surround it with back ticks or it will fail.

In my case the script above gives me:

[18:04:38.589] Action execution started.
[18:04:38.696] bytes 0-0/23389
[18:04:38.696] File Size: 22.8kb
[18:04:38.696] Action execution succeeded.

It is quite possible there is an easier way and i would love to hear it if there is.

2 Likes

Thank you Jason. This helps alot