How to convert a file to Base64
Base64 writes any bytes using 64 plain characters, so a file can travel inside JSON, an email, a URL or a line of CSS. To convert a file to base64, the tool reads its bytes in your browser and encodes every group of 3 bytes as 4 characters.
The built-in example is a 96 by 64 pixel gradient, 16,607 bytes. It becomes 22,144 Base64 characters, which is 33.3% more, and starts like this:
iVBORw0KGgoAAAANSUhEUgAAAGAAAABACAIAAABqVuVZAABApklEQVR42g3aiUv6/h8H8N9f8uVDEvery PNG starts with the same eight bytes, so every PNG in Base64 starts with iVBORw0KGgo. A PDF starts with JVBERi0 and a ZIP file with UEsDB. Seeing one of these is a quick way to tell what a string holds.
File to Base64 online: the four output formats
- Plain Base64: one unbroken line. This is what most JSON APIs and database fields expect.
- Data URI:
data:type;base64,..., where the type is the one your browser reports for the file, or the type found from the bytes when it reports none. You can use it in an HTMLsrcor a CSSurl(). - MIME, 76 per line: the same text with a line break every 76 characters, the style used in email bodies. Decoders ignore the breaks.
- URL-safe:
-and_replace+and/, and the=padding is dropped. It can sit in a URL or a file name without escaping.
Why the Base64 is a third bigger
Three bytes hold 24 bits. Base64 cuts those bits into four pieces of 6 bits, and each piece becomes one character. Four characters for every three bytes is 33.3% more, plus up to two = signs at the end. The summary shows the exact figure for your file. A line break every 76 characters adds a little more, while compression, if the receiving server uses it, can take back part of the cost.
For large files a data URI is rarely a good idea. It cannot be cached on its own, and the browser has to decode it each time. Use it for small icons and fonts. For a download or an upload, send the file itself.
Convert a zip, font or audio file to Base64
The tool does not care what the file is. A zip, a font, an audio clip or a spreadsheet all work the same way, because only the bytes matter. The type line is there to help you: it is read from the first bytes of the file, so it is right even when the name has the wrong extension.
If you only have a picture, Image to Base64 adds CSS, HTML and Markdown snippets. For a PDF, PDF to Base64 can also write a JSON field or a cURL command. To encode text instead of a file, use Base64 Encode.
The SHA-256 hash
The summary includes a SHA-256 hash of the original file, calculated by your browser. If you send the Base64 to someone, they can decode it, hash the result and compare. When the two hashes match, every byte arrived. The hash is of the file, not of the Base64 text, and it needs a secure page (https) to be calculated.
File to Base64 on the command line and in code
base64 -w 0 report.docx > report.b64 # Linux, one line
base64 -i report.docx -o report.b64 # macOS[Convert]::ToBase64String([IO.File]::ReadAllBytes("report.docx"))const base64 = require("fs").readFileSync("report.docx").toString("base64");To turn Base64 back into a file, use Base64 to File, which also finds the type, shows a preview and warns about programs.
Limits and privacy
- Files up to 20 MB are accepted. The result is about a third larger, and very long strings can be slow to paste into editors or send to an API.
- The preview shows the first 20,000 characters. Copy and Download include the whole string.
- Swap needs the file to be 10 MB or smaller, which is the limit of Base64 to File.
The file is read and encoded in your browser. It is not uploaded or stored. See the Privacy Policy for details.