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?