How to get the actual file size in bytes?

Hello,

I have a workflow that retrieves email attachments via IMAP. In the binary object corresponding to the file, I can access properties like “filename”, “filesize”, etc. However, the “filesize” value is given in KB. How can I get the value in bytes (B)?

I need this information in order to send the file to an API that requires the exact file size in bytes.

1 Like

You can try this expression

{{ $input.item.binary.data.data.length * 3 / 4 }}


Why Base64 is 33% larger

Base64 encoding converts binary data (files) into text format that’s safe to transmit.

The math:

  • Original data: 8 bits per byte
  • Base64: 6 bits per character
  • Ratio: 8 ÷ 6 = 1.33 (33% larger)

Is this common knowledge?

For developers: YES - it’s basic knowledge For regular users: NO - most people don’t need to know this

Where you see Base64:

  • Email attachments
  • Web images (data:image/png;base64,...)
  • API file transfers
  • n8n binary data

Quick conversion:

Real file size ≈ Base64 length × 0.75

So your 5660 length = ~4245 bytes = ~4.1 KB

It’s totally normal to learn this when working with file processing tools like n8n!


Found this way to calculate and seems working fine.

1 Like

Hello @darrell_tw,

Thank you for your help.

I followed your suggestion, but unfortunately, it doesn’t work on my side.

As you can see in the screenshot below, the file size is 24 kB, but the calculated total_size value is 9.75.

Hello,

After some investigations, I have found a solution by adding a node code with the following instructions :

const buffer = await this.helpers.getBinaryDataBuffer(0, ‘data’);

return {
fileSize: buffer.length
};

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.