Base64 Files
Deep Dive

Blob URL vs Data URL

Two ways to reference file data in a browser — similar in appearance, completely different in mechanism. This article explains the difference and helps you decide which to use.

What Are They?

Data URL

data:image/png;base64,iVBORw0KGgo…

The file's complete data is Base64-encoded and written directly into the URL. The URL itself is the data — you can copy it, paste it, store it in a database, or embed it in HTML or CSS.

Blob URL

blob:https://example.com/550e8400-e29b…

A temporary reference the browser creates for a Blob or File object in memory. The URL contains no data — it is just a pointer, valid only in the current page.

Side-by-Side Comparison

AspectData URLBlob URL
Formatdata:image/png;base64,iVBOR...blob:https://example.com/abc-123
Contains data?Yes — data is encoded in the URLNo — it is a reference to browser memory
Shareable / copyable?Yes — the URL itself is the dataNo — only valid in the current page
Memory usageHigh — Base64 string ≈ 1.33× fileLow — just an internal reference
Large file supportPoor — size bloat + browser slowdownGood — works at any size
LifetimePermanent (as long as the string exists)Expires on page close or revokeObjectURL()
Embed in HTML / CSSYes — inline directlyNo — invalid after page refresh
Requires manual cleanupNoYes — call revokeObjectURL()

When to Use Each

Use Data URL when:

  • Embedding small images in HTML / CSS (< 5 KB)
  • Embedding images in HTML emails (Blob URL not supported)
  • Inlining a small @font-face font
  • Sending small attachments in a JSON API
  • File data that needs to be copied, stored, or transmitted
  • Inlining SVG favicons in <link>

Use Blob URL when:

  • Previewing uploaded images or videos in the browser
  • Triggering file downloads (<a download>)
  • Previewing a canvas export
  • Temporarily displaying the result of a large file operation
  • MediaSource API for streaming playback
  • Temporary URLs while working with Blob data

Code Examples

Blob URL — Preview + Download
input.addEventListener('change', (e) => {
  const file = e.target.files[0]
  const url  = URL.createObjectURL(file)

  // Preview image
  img.src = url

  // Trigger download
  const a    = document.createElement('a')
  a.href     = url
  a.download = file.name
  a.click()

  // Release when done
  URL.revokeObjectURL(url)
})
Data URL — Embed Image in HTML
const reader = new FileReader()
reader.onload = (e) => {
  const dataUrl = e.target.result
  // Embed in HTML, CSS, JSON, or store
  img.src = dataUrl
  // dataUrl: "data:image/png;base64,iVBOR..."
}
reader.readAsDataURL(file)

Remember to Release Blob URLs

Every call to URL.createObjectURL() creates an internal reference to the Blob data inside the browser. Even if the JavaScript variable has been garbage collected, the reference remains and the data cannot be freed.

Call URL.revokeObjectURL(url) at the right moment to release the reference:

ScenarioWhen to revoke
Triggering a downloadImmediately after clicking (after the <a> click event)
Image previewWhen the user selects a new file, or when the preview component unmounts
Video previewWhen video playback ends, or when the component unmounts
React componentIn the cleanup function of useEffect

Related Tools

Frequently Asked Questions

What is the core difference between a Blob URL and a Data URL?

A Data URL encodes the file data into the URL itself (as a Base64 string) — it can be copied, transmitted, and stored persistently. A Blob URL is a temporary reference to Blob data in browser memory; the URL contains no data, is only valid in the current page, and expires when the page is closed.

Which is better for previewing images?

Blob URL is better. URL.createObjectURL() does not copy the data, so memory usage stays close to the original file size. FileReader.readAsDataURL() generates a Base64 string roughly 1.33× the original file size. The difference grows with file size.

Why can HTML emails only use Data URLs?

Email clients render HTML outside the browser context and cannot resolve Blob URLs (the blob: protocol is browser-private). Data URLs embed the image data directly in the HTML, so an email client can parse them independently — making them the only option for embedded images in email.

What happens if I never call revokeObjectURL?

The browser holds an internal reference to the Blob data indefinitely. Even after the JavaScript variable is garbage collected, the data cannot be freed. If a page creates many Blob URLs without releasing them, memory grows continuously until the page is closed.

Can I store a Blob URL in a database or localStorage?

No — there is no point. A Blob URL is only valid within the lifetime of the page that created it. Even if the URL string is preserved, the data it pointed to is gone after a page refresh. For persistence, use a Data URL or upload the file to a server first.

How do I convert a Blob URL to a Data URL?

Fetch the Blob URL to get the Blob, then use FileReader.readAsDataURL(): fetch(blobUrl).then(r => r.blob()).then(blob => { const reader = new FileReader(); reader.onload = e => console.log(e.target.result); reader.readAsDataURL(blob) })