TheFileTools

How to convert a file to Base64 or a data URI

Turn a small image, font or other file into Base64 text for HTML, CSS, JSON or an API request.

Base64 writes any file as plain letters, digits and a few symbols. Developers need it to place a small image directly inside HTML or CSS, to put a file into a JSON request, or to paste a binary file into a place that accepts only text.

Steps

  1. Open File to Base64.
  2. Choose the file. Any type works, up to 8 MB. It is encoded on your device.
  3. Decide about Include the data: prefix, which is ticked at the start. See below for which form you need.
  4. Press Copy to put the whole text on the clipboard, or download it as a .txt file.

The page shows only the beginning of long results. Copy and the download always contain the complete text.

With or without the prefix

  • With the prefix you get a data URI: the word data, the file type, the word base64 and then the encoded content, all in one line. This form can be pasted straight into the src of an image tag or into url() in a stylesheet.
  • Without the prefix you get the bare Base64 text. APIs, JSON fields and configuration files usually expect this form.

The file type in the prefix is the one your browser reports for the file. If the browser does not know the type, a generic one (application/octet-stream) is used, and you may need to correct it by hand.

Keep it small

Base64 makes data about a third larger than the original file, and an embedded picture cannot be cached separately by the browser. Embedding suits icons and small graphics of a few KB, not photos. Shrink the picture first with Compress image or Resize image.

Related tools

For plain text instead of a file, Base64 encode is the right tool, and Base64 decode turns Base64 back into readable text. Remember that Base64 is not encryption. Anyone can decode it.