## Why Files Break in Transit
A PNG file is not text. It is a stream of bytes, many of them unprintable. The moment you pipe that file through a system that assumes text — a JSON field, an XML node, an email body, a form field — the non-printable bytes get mangled, escaped, or dropped. The file arrives broken.
Base64 fixes this by turning every binary byte into a printable ASCII character. You base64 encode a file, transfer the resulting string, and base64 decode it back to a file on the other end. The bytes survive. The format survives. The checksum matches.
This is not a theoretical problem. I have seen a signed PDF arrive at a client with its first 12 bytes replaced by Unicode replacement characters because someone tried to send it as a raw string in a JSON field. The signature was invalid. The file would not open. The fix was a one-line change to Base64-encode before sending and Base64-decode on receipt. The problem was not the signing logic. It was the transport.
Base64 Encode a File in the Browser
Read the file with a FileReader, then encode the result:
javascript async function fileToBase64(file) { return new Promise((resolve) => { const reader = new FileReader(); reader.onload = () => resolve(reader.result.split(",")[1]); reader.readAsDataURL(file); }); }
const b64 = await fileToBase64(myFileInput.files[0]); // "iVBORw0KGgoAAAANSUhEUgAAAfQAAAH..."
The output is a long string of Base64 characters. No null bytes. No quotes to escape. You can drop it straight into a JSON payload, a form field, or a database text column.
Base64 Decode to a File
On the receiving side, turn the string back into bytes and trigger a download:
javascript function base64ToFile(b64, filename, mime) { const bytes = Uint8Array.from(atob(b64), c => c.charCodeAt(0)); const blob = new Blob([bytes], { type: mime }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = filename; a.click(); URL.revokeObjectURL(url); }
Call it with the string, a filename, and a MIME type. The original file lands in the downloads folder, byte-for-byte identical to what you encoded. Run a checksum on both sides and they will match.
Convert a File to a Base64 String in Node.js
Server-side, the Buffer API does the work:
javascript import { readFileSync } from "fs";
const bytes = readFileSync("report.pdf"); const b64 = bytes.toString("base64"); // "JVBERi0xLjQKJcKZAwYKK..."
And back:
javascript import { writeFileSync } from "fs";
const restored = Buffer.from(b64, "base64"); writeFileSync("report-restored.pdf", restored);
No streams. No temp files. One function call each way. The Buffer API handles the encoding and decoding natively, so there is no dependency to install and no library to maintain.
A Real Transfer: Small File in a JSON API
Imagine a signing service that returns a signed PDF. Instead of returning a binary download URL, it returns the file inline:
json { "status": "signed", "filename": "contract.pdf", "mime": "application/pdf", "data": "JVBERi0xLjQKJcKZAwYKK..." }
The client decodes data and saves the file. One request. No follow-up download. No CORS dance. For files under a megabyte or two, this is simpler than a multipart upload and a separate fetch. The client gets everything it needs in a single response.
The Size Tax
Base64 is not free. The encoded string is roughly 4/3 the size of the original file. A 1 MB PDF becomes a 1.33 MB string. For a 10 MB video, that is 13.3 MB of Base64 to ship through your API.
That tax is fine for small files and one-off transfers. It is a poor choice for large media. For those, use a direct upload to object storage and pass a URL instead. If you are not sure where the line is, encode files to Base64 with the NovelCrypt converter and check the output length before you commit to the approach. A good rule of thumb: if the Base64 string is longer than a few hundred kilobytes, reconsider.
Common Pitfalls
**Data URL prefix.** readAsDataURL returns data:<mime>;base64,<string>. Strip everything before the comma if you only want the Base64 part. If you forget this, your downstream decoder will choke on the prefix.
**Memory.** Holding a 50 MB Base64 string in memory is fine on a server and painful in a browser tab. Watch your sizes. Mobile browsers will crash on large strings.
**MIME type.** Decoding Base64 gives you bytes, not a file type. You must carry the MIME type alongside the string, or the receiver cannot reconstruct the file correctly. A PDF decoded as application/octet-stream will download but may not open.
**Checksums.** If integrity matters, hash the original bytes and verify after decode. Base64 itself has no error detection. A single corrupted character changes the output but produces no error.
When to Use a Base64 File Converter
Reach for it when you need to move a binary file through a text-only channel and the file is small enough that the 33% size increase is acceptable. Avoid it for large media, for streaming, or for anything where you need secrecy — Base64 is reversible by anyone. For the security angle, see Base64 is not encryption.
Quick Reference
- Browser encode: FileReader.readAsDataURL, strip the prefix. - Browser decode: atob to bytes, wrap in a Blob, download. - Node encode: Buffer.from(bytes).toString("base64"). - Node decode: Buffer.from(b64, "base64"). - Size: output is ~33% larger than input. - Always carry the MIME type with the string.
Try it yourself with the NovelCrypt Base64 file converter, and for the related image workflow read converting images to Base64 data URIs.,