0

If a user uploads a file (e.g. an mp3), using a standard <input type="file"/>, I will store their file in the DB as a Base64 string.

However, I realised that the size of the Base64 string will be bigger than the size of the file.

For example, if the file the user uploads is 3984547 bytes I then convert it into a Base64 string and then calculate the size of that string with:

function base64bytes(fileAsBase64) {
  return (new TextEncoder().encode(fileAsBase64)).length
}

and the value returned is 5312755 bytes.

So if the user uploads a ~3.9Mb file, the Base64 string I will store in the DB will be ~5.3Mb.

My problem is that I want to only allow Base64 strings of up to 100Mb.

But I don't know how to let the user know about it.

If I tell the user that I only allow files of up to 100Mb that will be misleading. They might upload a file that is 98Mb on their machine but my Base64 string will be probably bigger than 100Mb so I will reject their file and confuse them.

According to this answer the Base64 version of an image is always ~137% bigger than the original image file. But does this ratio only stand for images ? My files could be of any type.

Is this ratio safe enough to use on any type of file or would you suggest some other solution to my problem?

Kawd
  • 4,122
  • 10
  • 37
  • 68
  • 1
    Base64 encodes 3 bytes into 4 characters, regardless of content. – tevemadar May 26 '22 at 22:32
  • So the length of the resulting Base64 string will always be `((originalFileBytes / 3) * 4) + base64prefix.length`? (if I go the math right..) – Kawd May 26 '22 at 22:52
  • Yes. There may be some linebreaks too if you ask the encoder to generate some. Side note: I'm not sure why don't you store binary content directly, relational databases usually have some type for that. – tevemadar May 26 '22 at 22:56
  • I want to cut up the Base64 string into pieces and store each piece separately in the DB and then merge them back together when I want to. Can I do this with Binary data ? – Kawd May 26 '22 at 23:01
  • Sure. Data is data, bytes are bytes, whether it’s ASCII or binary. – Dave Newton Jun 04 '22 at 22:09

1 Answers1

0

The base64 version of a file is at least 33% larger than the original file. The increase depends on the file size, the smaller the file, the larger the increase.

Allowing an upload file size of 75MB would lead to a base64 encoded files of around 99.75MB.

mic
  • 3
  • 3