Blog · Base64 How-To

How to Encode a File to Base64

Convert any file to Base64 in seconds. Drag, drop, and get the encoded string instantly. Works with images, PDFs, JSON, and more.

Need to embed an image in HTML, send a file through a JSON API, or store binary data as text? Encoding a file to Base64 converts any binary file into a safe, printable ASCII string. Here is how to do it, without installing anything.

Method 1: Use Our Online Tool (No Setup)

The fastest way: open our free Base64 encoder, drag your file onto the page, and the Base64 output appears instantly. Works for images, documents, code files, anything. No account, no upload to any server. Everything runs locally in your browser.

  1. Go to base64go.com
  2. Drag and drop your file onto the input area
  3. The Base64 string appears in the output panel
  4. Click Copy or Download to save it

Method 2: JavaScript (Browser)

Use the FileReader API to convert a file to Base64 in the browser:

function fileToBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = () => resolve(reader.result);
    reader.onerror = reject;
    reader.readAsDataURL(file);
  });
}

// Output: "data:image/png;base64,iVBORw0KG..."

Method 3: Python

import base64

with open("image.png", "rb") as f:
    encoded = base64.b64encode(f.read()).decode("utf-8")

# encoded is now a plain Base64 string

Method 4: Command Line (Linux / Mac / WSL)

# Encode a file
base64 -i input.png -o output.b64

# Or output directly to terminal
base64 image.png

Common Use Cases

  • Data URIs in HTML/CSS. Embed small images directly in your code: <img src="data:image/png;base64,...">
  • JSON API payloads. Send binary files through REST APIs that only accept JSON.
  • Email attachments. MIME uses Base64 to encode attachments in email bodies.
  • Browser localStorage. Store small files client-side by encoding them as Base64 first.

Try it now. Drag and drop any file.

No upload, no sign-up. Everything happens in your browser.

Open Base64 Tool