โ† Back to Blog

How to Base64 Encode Any File

2026-04-12 ยท 5 min read

โ† Back to Blog

How to Base64 Encode Any File

ยท 5 min read

Use Cases for File Base64 Encoding

Base64 encoding files is very useful in scenarios such as: transmitting binary files through REST APIs (JSON format); storing file contents in JSON or YAML configuration files; storing certificates and keys in Kubernetes Secret resources; packaging multiple files into a single text format for transmission; and sending files through APIs that don't support multipart form uploads.

Base64 encoding allows any file (whether PDF, image, audio, video, or executable) to be treated as a plain text string, greatly simplifying the complexity of cross-system data exchange.

Encoding Files with Command Line Tools

# Linux / macOS
# ็ผ–็ ๆ–‡ไปถๅนถ่พ“ๅ‡บๅˆฐๅฑๅน• / Encode file and output to screen
base64 document.pdf

# ็ผ–็ ๆ–‡ไปถๅนถไฟๅญ˜ๅˆฐๆ–ฐๆ–‡ไปถ / Encode file and save to new file
base64 document.pdf > document.pdf.b64

# ไธๅซๆข่กŒ็ฌฆ่พ“ๅ‡บ๏ผˆ้€‚ๅˆ API ไฝฟ็”จ๏ผ‰/ Output without line breaks (for API use)
base64 -w 0 document.pdf > document.pdf.b64

# ่งฃ็  / Decode
base64 -d document.pdf.b64 > restored.pdf

# Windows PowerShell
[Convert]::ToBase64String([IO.File]::ReadAllBytes("document.pdf")) |
  Out-File -Encoding ascii document.pdf.b64

# ่งฃ็  / Decode
$bytes = [Convert]::FromBase64String((Get-Content document.pdf.b64))
[IO.File]::WriteAllBytes("restored.pdf", $bytes)

Encoding Any File in Python

import base64
import json
from pathlib import Path

def encode_file_to_base64(file_path):
    """่ฏปๅ–ๆ–‡ไปถๅนถ่ฟ”ๅ›ž Base64 ๅญ—็ฌฆไธฒ"""
    with open(file_path, 'rb') as f:
        return base64.b64encode(f.read()).decode('ascii')

def decode_base64_to_file(base64_str, output_path):
    """ๅฐ† Base64 ๅญ—็ฌฆไธฒ่งฃ็ ๅนถๅ†™ๅ…ฅๆ–‡ไปถ"""
    data = base64.b64decode(base64_str)
    with open(output_path, 'wb') as f:
        f.write(data)
    return len(data)

# ๅฐ†ๆ–‡ไปถๅตŒๅ…ฅ JSON / Embed file in JSON
def file_to_json_payload(file_path):
    path = Path(file_path)
    return {
        "filename": path.name,
        "size": path.stat().st_size,
        "content": encode_file_to_base64(file_path)
    }

payload = file_to_json_payload("report.pdf")
json_str = json.dumps(payload)
print(f"JSON payload size: {len(json_str)} chars")

Encoding Files in Node.js

const fs = require('fs');
const path = require('path');

// ๅŒๆญฅๆ–นๅผ / Synchronous
function encodeFileSync(filePath) {
  return fs.readFileSync(filePath).toString('base64');
}

// ๅผ‚ๆญฅๆ–นๅผ๏ผˆๆŽจ่็”จไบŽ็”Ÿไบง๏ผ‰/ Async (recommended for production)
async function encodeFileAsync(filePath) {
  const data = await fs.promises.readFile(filePath);
  return data.toString('base64');
}

// ๆตๅผๅค„็†ๅคงๅž‹ๆ–‡ไปถ / Stream large files
const { createReadStream } = require('fs');
const { Transform } = require('stream');

function encodeFileStream(inputPath, outputPath) {
  const readStream = createReadStream(inputPath);
  const writeStream = fs.createWriteStream(outputPath);
  const base64Transform = new Transform({
    transform(chunk, encoding, callback) {
      callback(null, chunk.toString('base64'));
    }
  });
  readStream.pipe(base64Transform).pipe(writeStream);
}

File Encoding in Kubernetes Secrets

Kubernetes Secret resources use Base64 to store sensitive data (like certificates, keys, and config files). In a Secret's YAML file, every value under the data field must be Base64-encoded. Note: the kubectl create secret command automatically handles Base64 encoding, but when manually writing YAML you need to encode yourself.

# ็”Ÿๆˆ Kubernetes Secret ็š„ YAML
# Generate Kubernetes Secret YAML
CA_CERT=$(base64 -w 0 ca.crt)
TLS_KEY=$(base64 -w 0 server.key)

cat  secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: tls-secret
type: kubernetes.io/tls
data:
  tls.crt: ${CA_CERT}
  tls.key: ${TLS_KEY}
EOF

kubectl apply -f secret.yaml

Considerations for Large Files

When Base64-encoding large files, pay special attention to memory usage. A 100MB file becomes about 133MB after Base64 encoding, which can be a problem in memory-constrained environments. For files larger than several tens of MB, streaming is recommended rather than reading the entire file into memory at once.

Another consideration is transmission efficiency. Base64's 33% size overhead is acceptable for small files, but for large media files, using multipart/form-data format to directly upload binary data is recommended โ€” no size overhead, and most web frameworks and cloud storage services support it natively.

Verifying the Integrity of Encoded Results

Before using a file's Base64-encoded result in a production system, it's recommended to verify its integrity: decode the Base64 string and compare with the original file's hash (MD5 or SHA-256) to ensure data wasn't corrupted during encoding/decoding.

import base64
import hashlib

def verify_base64_file(original_path, base64_str):
    """้ชŒ่ฏ Base64 ๅญ—็ฌฆไธฒๆ˜ฏๅฆไธŽๅŽŸๆ–‡ไปถไธ€่‡ด"""
    # ่ฎก็ฎ—ๅŽŸๅง‹ๆ–‡ไปถ็š„ SHA-256
    with open(original_path, 'rb') as f:
        original_hash = hashlib.sha256(f.read()).hexdigest()

    # ่งฃ็  Base64 ๅนถ่ฎก็ฎ—ๅ“ˆๅธŒ
    decoded = base64.b64decode(base64_str)
    decoded_hash = hashlib.sha256(decoded).hexdigest()

    if original_hash == decoded_hash:
        print("Integrity verified: hashes match")
        return True
    else:
        print(f"MISMATCH! Original: {original_hash}, Decoded: {decoded_hash}")
        return False

Try the online tool now โ€” no installation, completely free.

Open Tool โ†’

Try the free tool now

Use Free Tool โ†’