Reputation: 675
I have a file encoded in Base64 with a certain file length. How do I get the size in bytes?
Example:
var size= canvas.toDataURL();
console.log(resizeCanvasURL.length);
// -> 132787 base64 length
Upvotes: 7
Views: 19307
Reputation: 41
const stringLength = resizeCanvasURL.length
const sizeInBytes = 4 * Math.ceil(stringLength / 3) * 0.5624896334383812;
const sizeInKb = sizeInBytes / 1000;
OR
const stringLength = resizeCanvasURL.length
const sizeInBytes = stringLength * (3 / 4) - 2;
const sizeInKb = sizeInBytesNew / 1000;
Upvotes: 0
Reputation: 661
The answer from mishik is wrong.
Base64 is filled up with =-chars (to have the right amount of bits). So it should be
var byteLength = parseInt((str).replace(/=/g,"").length * 0.75));
Upvotes: 8
Reputation: 10003
Each symbol in Base64 encoding holds 6 bits of information. Each symbol in normal file hold 8 bits. Since after decoding you have the same amount of information you need:
normal file size - 1000 bytes
base64 file size - 1333 bytes
Upvotes: 10