Base64 File Encoder
Encode any file to Base64 — PDF, ZIP, images, and more
Drop any file here or click to upload
Any file type — PDF, ZIP, images, binary files
What Is Base64 File Encoding?
Base64 file encoding converts binary files — PDFs, ZIPs, images, executables, fonts, audio — into plain ASCII text using the alphabet defined in RFC 4648. Every three bytes of binary input become four Base64 characters, producing a text representation that can travel through channels designed exclusively for text: JSON API payloads, XML documents, email bodies, HTML attributes, and environment variables.
Unlike text-to-Base64 encoding (which operates on strings), file-to-Base64 encoding reads the raw byte stream of any file regardless of its format. A PDF's binary headers, a ZIP's compressed blocks, and a PNG's pixel data are all treated identically: as a sequence of octets to be re-encoded. The result is always valid ASCII, safe to embed anywhere that accepts printable characters.
The trade-off is size: Base64 output is approximately 33% larger than the original binary. For a 1 MB PDF, that means roughly 1.33 MB of Base64 text. This overhead is the cost of representing 8-bit bytes with 6-bit characters and is inherent to the encoding — no implementation can avoid it. Despite this, Base64 file encoding remains the standard method for embedding binary data in text-only formats, specified across MIME (RFC 2045), data URIs (RFC 2397), and JSON Web Tokens (RFC 7519).
Why Use This Base64 File Encoder?
This tool encodes files to Base64 directly in your browser using the FileReader API. No file is uploaded to a server — the entire conversion happens client-side in JavaScript.
Base64 File Encoding Use Cases
Base64 File Size Overhead
Base64 encoding increases data size by exactly one third. Every 3 input bytes produce 4 output characters (each representing 6 bits). Padding characters (=) are added when the input length is not a multiple of 3. The table below shows the relationship between original file size and encoded output size.
| File Size | Raw Bytes | Base64 Bytes | Overhead |
|---|---|---|---|
| 1 KB | 1,024 B | 1,368 B | +33.3% |
| 10 KB | 10,240 B | 13,656 B | +33.4% |
| 100 KB | 102,400 B | 136,536 B | +33.3% |
| 1 MB | 1,048,576 B | 1,398,104 B | +33.3% |
| 5 MB | 5,242,880 B | 6,990,508 B | +33.3% |
| 10 MB | 10,485,760 B | 13,981,016 B | +33.3% |
Data URI and MIME Type Reference
A data URI embeds file content directly in HTML, CSS, or JavaScript using the format data:[MIME type];base64,[encoded data]. The MIME type tells the browser how to interpret the decoded bytes. Below are common file extensions, their MIME types, and the corresponding data URI prefix.
| Extension | MIME Type | Data URI Prefix |
|---|---|---|
| application/pdf | data:application/pdf;base64,... | |
| .zip | application/zip | data:application/zip;base64,... |
| .png | image/png | data:image/png;base64,... |
| .jpg | image/jpeg | data:image/jpeg;base64,... |
| .gif | image/gif | data:image/gif;base64,... |
| .svg | image/svg+xml | data:image/svg+xml;base64,... |
| .woff2 | font/woff2 | data:font/woff2;base64,... |
| .mp3 | audio/mpeg | data:audio/mpeg;base64,... |
| .wasm | application/wasm | data:application/wasm;base64,... |
| .bin | application/octet-stream | data:application/octet-stream;base64,... |
Code Examples
These runnable examples show how to read a binary file and encode it to Base64 in JavaScript, Python, Node.js, bash, and Go. Each snippet produces standard Base64 (RFC 4648 Section 4) with padding.
// Read a file from an <input> element and encode to Base64
const input = document.querySelector('input[type="file"]')
input.addEventListener('change', (e) => {
const file = e.target.files[0]
const reader = new FileReader()
reader.onload = () => {
const base64 = reader.result.split(',')[1]
console.log(base64) // → "JVBERi0xLjQK..." (raw Base64, no data URI prefix)
}
reader.readAsDataURL(file)
})
// Convert a Blob to Base64 with async/await
async function blobToBase64(blob) {
const buffer = await blob.arrayBuffer()
const bytes = new Uint8Array(buffer)
let binary = ''
bytes.forEach(b => binary += String.fromCharCode(b))
return btoa(binary) // → standard Base64 string
}import base64
from pathlib import Path
# Encode a file to Base64
file_bytes = Path('document.pdf').read_bytes()
encoded = base64.b64encode(file_bytes).decode('ascii')
print(encoded[:40]) # → "JVBERi0xLjQKJeLjz9MKMSAwIG9iago8PA..."
# Write encoded output to a text file
Path('document.b64.txt').write_text(encoded)
# Build a data URI from a file
mime_type = 'application/pdf'
data_uri = f'data:{mime_type};base64,{encoded}'
print(data_uri[:60]) # → "data:application/pdf;base64,JVBERi0xLj..."import { readFileSync, writeFileSync } from 'fs'
// Encode a file to Base64
const buffer = readFileSync('archive.zip')
const base64 = buffer.toString('base64')
console.log(base64.length) // → 1398104 (for a ~1 MB file)
// Save Base64 output to a file
writeFileSync('archive.b64.txt', base64)
// Build a data URI
const mime = 'application/zip'
const dataUri = `data:${mime};base64,${base64}`# Encode a file to Base64 (macOS / Linux) base64 < document.pdf > document.b64.txt # Encode with no line wrapping (GNU coreutils) base64 -w 0 < document.pdf > document.b64.txt # Encode and copy to clipboard (macOS) base64 < image.png | pbcopy # Encode with OpenSSL (available everywhere) openssl base64 -in archive.zip -out archive.b64.txt # Pipe directly into curl for API upload base64 -w 0 < photo.jpg | curl -X POST -d @- https://api.example.com/upload
package main
import (
"encoding/base64"
"fmt"
"os"
)
func main() {
data, err := os.ReadFile("document.pdf")
if err != nil {
panic(err)
}
encoded := base64.StdEncoding.EncodeToString(data)
fmt.Println(len(encoded)) // → 1398104 (for a ~1 MB file)
// Write to file
os.WriteFile("document.b64.txt", []byte(encoded), 0644)
}