Guide

Base64 for text, files, and Data URLs: what changes

Distinguish UTF-8 text, file bytes, and Data URLs so you do not copy prefixes, lose media types, or interpret binary as text.

by Tools in a Tab · Published on · Reviewed on

Short answer

A Base64 string does not identify whether it contains text, an image, or any other file. Text needs a known character encoding such as UTF-8; files require their original bytes and media type to be preserved. A Data URL adds metadata and a comma before the data, and that prefix is not part of the Base64 payload.

Three similar-looking but different values

Base64 for UTF-8 text contains the bytes produced from its characters. Base64 for a PNG contains the file’s binary bytes directly. Both use the same alphabet, but only the first should be decoded into a string with UTF-8.

A Data URL also packages a content type:

data:image/png;base64,iVBORw0KGgo...

The Base64 portion starts after the first comma. data:image/png;base64, is a header describing the media and encoding. Passing the complete URL to a decoder that expects raw Base64 may cause an error or, in a permissive decoder, create unwanted bytes from characters in the prefix.

When to use a text converter

The Base64 and Base64URL converter is intentionally for UTF-8 text. It suits a configuration string, a JSON fragment handled as text, or an interoperability test. It rejects decoded bytes that are not valid UTF-8 instead of displaying misleading replacement characters.

It is not an image viewer or general file converter. A valid PNG can be represented as Base64, but interpreting its bytes through UTF-8 provides no meaningful result and should be allowed to fail.

Identifying and splitting a Data URL

RFC 2397 defines the form as data:[mediatype][;base64],data. Find the comma ending the metadata, verify the presence of ;base64, and decode only the following portion. Without ;base64, the data uses percent encoding rather than Base64.

Do not trust the declared media type as proof. An untrusted source can label content image/png while supplying a different format. A download or preview feature also needs file-signature checks, size limits, and an allowed-type policy.

Text versus file bytes

For text, a useful contract states UTF-8 + Base64 or UTF-8 + Base64URL and defines padding. For a file, retain its name when needed, media type, expected size, and checksum. After decoding, write the bytes directly rather than passing through an intermediate Unicode string.

This distinction prevents the faulty path “binary → string → Base64,” which can change byte values. In a browser, File.arrayBuffer() or Blob.arrayBuffer() exposes bytes; reserve TextEncoder for actual text.

Size and memory cost

Base64 increases size by roughly one third because four characters represent every three bytes, and a Data URL adds its header. Embedding a large image in HTML or JSON can also create temporary copies and block the main browser thread during conversion.

For large files, a regular URL, object storage, or a binary upload is usually better. Data URLs are practical for small self-contained resources, not as a universal replacement for files.

Checklist before decoding

Identify every layer first: raw Base64 or Data URL, standard or URL-safe alphabet, text or binary, UTF-8 or another encoding. Validate syntax and size, remove a verified prefix when present, and preserve the bytes. Convert to text only when the contract says the content is text. Base64 transports information but does not supply a filename, trustworthy media type, security, or character encoding on its own.