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.

🛡
Privacy-First Processing
Your file never leaves your device. The FileReader API reads the file into memory locally, and the Base64 output is generated entirely in the browser. No network request is made.
Instant Drag-and-Drop
Drop any file onto the tool and see the Base64 output immediately. No waiting for uploads, no progress bars, no file size limits imposed by a server.
📁
Any File Format
Encode PDFs, ZIPs, images, fonts, audio files, WebAssembly modules, or any other binary format. The encoder treats all files as raw byte streams — format does not matter.
🔒
No Account Required
Use the tool immediately without signing up, logging in, or accepting cookies. The output is ready to copy or download as a .b64.txt file with one click.

Base64 File Encoding Use Cases

Frontend Developer
Embed small icons, fonts, or SVGs directly in CSS or HTML as data URIs to eliminate extra HTTP requests. A 2 KB icon inlined as Base64 saves a network round-trip that would add 50-200 ms of latency.
Backend Engineer
Include file attachments in JSON API payloads when the transport protocol does not support multipart uploads. Encode PDFs, reports, or signed documents before sending them as string fields in REST or GraphQL responses.
DevOps / Infrastructure
Store binary configuration files (TLS certificates, SSH keys, license files) as Base64 strings in environment variables, Kubernetes Secrets, or Terraform tfvars where raw binary values are not permitted.
QA / Test Engineer
Generate Base64 file fixtures for automated tests that verify upload endpoints, email attachment handling, or document processing pipelines without managing binary test files in version control.
Data Engineer
Serialize binary blobs (Parquet metadata, Protobuf schemas, small binary assets) into Base64 for storage in JSON-based data catalogs, configuration stores, or migration scripts.
Student / Learner
Observe how binary files become text by encoding small files and inspecting the output. Compare the Base64 length to the original file size to verify the 33% overhead described in RFC 4648.

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 SizeRaw BytesBase64 BytesOverhead
1 KB1,024 B1,368 B+33.3%
10 KB10,240 B13,656 B+33.4%
100 KB102,400 B136,536 B+33.3%
1 MB1,048,576 B1,398,104 B+33.3%
5 MB5,242,880 B6,990,508 B+33.3%
10 MB10,485,760 B13,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.

ExtensionMIME TypeData URI Prefix
.pdfapplication/pdfdata:application/pdf;base64,...
.zipapplication/zipdata:application/zip;base64,...
.pngimage/pngdata:image/png;base64,...
.jpgimage/jpegdata:image/jpeg;base64,...
.gifimage/gifdata:image/gif;base64,...
.svgimage/svg+xmldata:image/svg+xml;base64,...
.woff2font/woff2data:font/woff2;base64,...
.mp3audio/mpegdata:audio/mpeg;base64,...
.wasmapplication/wasmdata:application/wasm;base64,...
.binapplication/octet-streamdata: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.

JavaScript (browser)
// 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
}
Python
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..."
Node.js
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}`
CLI (bash)
# 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
Go
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)
}

Frequently Asked Questions

What is the maximum file size I can encode to Base64?
This tool runs entirely in your browser, so the limit depends on available memory. Modern browsers handle files up to 50-100 MB without issues. For files larger than that, use a command-line tool like base64 (macOS/Linux) or Python's base64 module, which process the file in chunks and do not load the entire output into memory at once.
How much larger is the Base64 output compared to the original file?
Base64 output is always approximately 33.3% larger than the input. Specifically, the formula is ceil(n / 3) * 4, where n is the number of input bytes. A 1 MB file produces about 1.33 MB of Base64 text. This overhead is inherent to the encoding and cannot be reduced.
Can I encode a file to Base64 and use it as a data URI?
Yes. Prepend the MIME type prefix to the Base64 string: data:application/pdf;base64, followed by the encoded content. Browsers will decode and render the data URI as if it were a regular file. Data URIs work in img src, link href, CSS url(), and anchor download attributes.
Is Base64 file encoding the same as encryption?
No. Base64 is an encoding, not encryption. It transforms binary data into a text representation that is trivially reversible — anyone with a Base64 decoder can recover the original file. Base64 provides no confidentiality, integrity, or authentication. If you need to protect file contents, encrypt the file first (for example with AES-256-GCM), then Base64-encode the ciphertext for transport.
Why does my Base64 string end with one or two = characters?
The = characters are padding. Base64 processes input in 3-byte groups, producing 4 output characters per group. When the file size is not a multiple of 3, one or two padding characters are appended so the output length is always a multiple of 4. One = means the last group had 2 bytes; two == means the last group had 1 byte.
How do I encode a file to Base64 from the command line?
On macOS and Linux, use the base64 command: base64 < file.pdf > file.b64.txt. On GNU/Linux, add -w 0 to suppress line wrapping. You can also use OpenSSL: openssl base64 -in file.pdf -out file.b64.txt. On Windows PowerShell: [Convert]::ToBase64String([IO.File]::ReadAllBytes('file.pdf')).
When should I use Base64 file encoding instead of multipart/form-data?
Use Base64 when the transport requires a text-only format — JSON APIs, XML-RPC, environment variables, or Kubernetes Secrets. Use multipart/form-data when uploading files through HTML forms or REST endpoints that support binary streams. Multipart is more efficient because it avoids the 33% size overhead, but it requires the server to parse multipart boundaries.