Base64 to File

Base64 to file: paste a Base64 string or data URI, see what kind of file it is from the first bytes, and download it with the right extension. ZIP, XLSX, DOCX, PDF, PNG, MP3 and others are recognized. The file is built in your browser and never uploaded.

Base64 to file
134 bytes · ZIP archive (.zip)
The file is built from your bytes in this browser and is not uploaded
What the decoded bytes are
Detected typeZIP archive
MIME typeapplication/zip
Size134 bytes
First bytes50 4B 03 04 14 00 00 00 00 00 00 60 21 5A 09 B7

Base64 to file: decode the string, then save the bytes

To turn a Base64 string back into a file, decode it to bytes and write those bytes to disk under a name with the right extension. Base64 stores no file name or type, so the extension has to come from somewhere else: the first bytes of the data, a MIME type from the source, or you. The converter above decodes in your browser, names the type it finds, and offers the download.

The bytes are identical to the original file. Base64 is a reversible way of writing any bytes with 64 text characters, with no compression or encryption, so a decoded ZIP is the same ZIP that was encoded, down to the last byte. If the original was 134 bytes, the Base64 is 180 characters, and the file you get back is again 134 bytes; the example loaded in the box is exactly that, a ZIP with one text file.

Finding the file type from the first bytes

Most file formats begin with a fixed byte pattern, called a signature or magic number. The converter compares the decoded bytes with a list of such patterns, and for a few text formats it reads the start of the text. Because Base64 maps three bytes onto four characters, the signature also shows in the string itself, and a given file type always starts with the same characters. The table shows the signatures the converter checks and the start of the Base64 that each produces.

UEsDBBQA decodes to a ZIP archive signatureThe Base64 characters U E s D B B Q A decode to the bytes 50 4B 03 04 14 00. The first four bytes are the signature of a ZIP archive.Base64 charactersUEsDBBQAdecoded bytes (hex), three bytes for every four characters50P4BK03·04·14·00·the same bytes as ASCII characterssignature → ZIP archive
UEsDBBQA is the start of the example string. It decodes to 50 4B 03 04 14 00; the bytes 50 4B are the letters PK, the mark of a ZIP archive, so the file is saved as .zip.
File signatures and how the Base64 string starts
TypeExtensionFirst bytes (hex)Base64 starts with
PDF document.pdf25 50 44 46 2DJVBERi
PNG image.png89 50 4E 47 0D 0A 1A 0AiVBORw0KGg
JPEG image.jpgFF D8 FF/9j/
GIF image.gif47 49 46 38 37 61R0lGODdh
ZIP archive.zip50 4B 03 04UEsDB
GZIP archive.gz1F 8BH4
7-Zip archive.7z37 7A BC AF 27 1CN3q8rycc
RAR archive.rar52 61 72 21 1A 07UmFyIRoH
MP3 audio (ID3 tag).mp349 44 33SUQz
Ogg container.ogg4F 67 67 53T2dnU
SQLite database.sqlite53 51 4C 69 74 65 20 66 6F 72 6D 61 74 20 33 00U1FMaXRlIGZvcm1hdCAzA
ELF executable.elf7F 45 4C 46f0VMR

The characters in the last column are only those that are completely fixed by the signature. The next one or two characters depend on the byte that follows, so a PNG string begins iVBORw0KGgo and then varies. When the match is exact, you can name the file by eye: UEsDB is a ZIP and JVBERi is a PDF, which also is the rule behind the Base64 to PDF page. A string that starts with /9j/ is a JPEG that you can preview on Base64 to image.

Excel, Word and PowerPoint files are ZIP archives

Office documents in the modern formats (XLSX, DOCX, PPTX) are ZIP archives with XML files inside, so their signature is the ZIP one and their Base64 starts with UEsDB like any other ZIP. What tells them apart is the folder inside: xl/ for a workbook, word/ for a document and ppt/ for a presentation. The converter reads the entry names in the first 4 KB of the data and picks the matching extension. A ZIP without such a folder stays a ZIP.

ZIP-based formats and the folder that identifies them
ExtensionFormatFolder in the archive
.xlsxExcel workbookxl/
.docxWord documentword/
.pptxPowerPoint presentationppt/
.zipPlain archivenone of the above

If the identifying entry names come later than the first 4 KB, which can happen when a large file is stored first, the file is reported as a ZIP. Change the extension box to xlsx, docx or pptx and download again; the bytes are the same, only the name changes, and the Office program will open it.

Where Base64 files come from

JSON cannot carry raw bytes, so APIs return documents, images and exports as Base64 strings inside a field, with names such as content, data or fileContent. Email works the same way: attachments in MIME messages are Base64, wrapped at 76 characters per line (RFC 2045). Databases and configuration files store small files as Base64 text for the same reason, and the Gmail API returns attachment data in the URL-safe variant, with - and _ in place of + and /.

All of these decode the same way. Copy the value without the quotation marks, keep the whole string, and paste it. Line breaks and spaces between characters are ignored, so wrapped MIME text pastes as it is, and the URL-safe alphabet is accepted without a switch. If the value sits in a JSON field, remove any backslash escapes of the slash (\/), which some servers add.

Data URIs and the declared MIME type

A data URI puts the type in front of the Base64: data:application/pdf;base64,JVBERi0… The converter strips the prefix, decodes the part after the comma and keeps the declared MIME type for comparison. When the declared type and the bytes disagree, for example a URI that says application/pdf but contains a PNG, a note appears and the extension follows the bytes, since the bytes are what the file really is.

How big the file will be

Base64 grows the data by a third: four characters for every three bytes, rounded up, and padding fills the last group. MIME wrapping adds a CRLF after each 76 characters, which makes it about 37 percent bigger than the file. Knowing the file size helps to spot a string that was cut off, because the decoded byte count in the status line must match the size the source reports.

File size and Base64 length
File size (bytes)Base64 charactersWith 76-character lines
1,0001,3361,370
100,000133,336136,844
1,000,0001,333,3361,368,422
10,000,00013,333,33613,684,212

Truncated and damaged strings

A Base64 string has a length of 4n, 4n + 2 or 4n + 3 characters (before padding). A length of 4n + 1 cannot come from whole bytes, so a character was lost or added, and the converter says so. A missing padding at the end is harmless and is reported only as a note. More serious is a string cut off in the middle: the length is valid, the bytes decode, and the file is incomplete. Signs are a size smaller than expected and, for a PDF, a missing %%EOF at the end.

Illegal characters are named with their positions. A common source is the + sign: in web forms and some URLs, + stands for a space, so a Base64 string passed through them comes back with spaces where the pluses were. Spaces are skipped when decoding, so the file loses those characters and breaks. Put the + signs back before decoding.

Base64 that was encoded twice

If the decoded result is readable text that looks like Base64 itself, the data was encoded twice, usually by a system that encodes an already encoded field. The converter detects this when the decoded bytes are Base64 characters only and offers a button to decode once more. The first decoding of a twice-encoded ZIP begins with UEsDB as text, and the second gives the ZIP. A ZIP encoded twice starts with VUVz, which is the Base64 of the text UEs, and a twice-encoded PDF starts with SlZCRVJp.

Opening the downloaded file

Open the file with the program for its type: an archive tool for a ZIP, Excel, Word or a PDF reader for documents. If an Office program reports the file as damaged, the extension is the first thing to check, because a DOCX saved with an xlsx extension may be refused or reported as damaged. The next is the size, then the string's end. Compare a hash of the downloaded file with one from the source when it publishes one; if the SHA-256 matches, the file is exactly what was encoded.

Safety when the string comes from someone else

Decoding cannot harm your computer, since it writes only bytes. What follows can: opening a macro-enabled Office file, running a program or unpacking an archive from an unknown source. The converter warns when the data starts like an executable (a Windows program or a Linux ELF file). Do not run such a file unless you know its origin; check the extension shown before you open anything, and for unexpected documents, use a viewer that does not run macros.

The same conversion in a script

Every language has a Base64 decoder that returns bytes. Write those bytes with the file API, in binary mode. Each snippet below saves the ZIP from the example as hello.zip, 134 bytes, which an archive tool opens. See the Node.js Buffer documentation for the encoding names.

Node.js
const fs = require("fs");
const b64 = "UEsDBBQAAAAAAABgIVoJtytxEgAAABIAAAAJAAAA...";  // the full string
fs.writeFileSync("hello.zip", Buffer.from(b64, "base64"));
PowerShell
$b64 = "..."   # the full string
[IO.File]::WriteAllBytes("$PWD\hello.zip", [Convert]::FromBase64String($b64))
Bash (GNU base64)
echo "$B64" | base64 -d > hello.zip

Keep the write in binary mode. Writing the decoded bytes as text in an encoding such as UTF-8 changes any byte above 127 and corrupts the file, which is the usual cause of an Office or ZIP file that opens as damaged when it was decoded in a script.

Large files

The converter holds the string and the bytes in the memory of the browser tab, so a file of many megabytes needs a lot of it, and the text box becomes slow to edit. For very large files use a script or the command line above, which can stream the data. A string of several hundred megabytes will not fit comfortably in any browser tab, and a file of that size is better shipped as a download than as Base64.

When the decoded data is text

Not every Base64 string is a file in the usual sense. If the decoded bytes are readable UTF-8 text, the converter shows a preview and suggests .txt. A string such as a JSON document, a configuration or a certificate in PEM form is text and is easier to read with Base64 decode, which also lets you choose the character encoding. A key or a hash is bytes with no type at all, and is better written as hex, as on the Base64 to hex page.

Typical mistakes when converting Base64 to a file

The frequent ones are copying only part of the string, including the quotation marks or a trailing comma from JSON, pasting the data URI prefix twice, and forgetting that the extension matters. Saving the result as .txt makes a ZIP unreadable until it is renamed. Also check the first characters before blaming the converter: a string that starts with eyJ is not a file but a JSON or JWT segment, and one that starts with a long run of A characters has a lot of zero bytes at the start, which is normal for some formats and suspicious for others.

Questions about converting Base64 to a file

How do I convert a Base64 string to a file?

Decode the string to bytes and save the bytes under a name with the right extension. Paste the string above, check the detected type, and press Download. The file is built in your browser and nothing is uploaded.

How can I tell what kind of file a Base64 string is?

Look at the first characters. UEsDB is a ZIP, and so is a DOCX, XLSX or PPTX. JVBERi is a PDF, iVBORw0KGgo is a PNG, /9j/ is a JPEG and H4sI is gzip. The converter reads the decoded bytes the same way and names the type.

Why did my Excel file download as a ZIP?

An XLSX file is a ZIP archive. The converter looks for the xl/ folder inside the first 4 KB to tell it from a plain ZIP; if the entry names are further in, set the extension to xlsx by hand and the file will open normally.

The downloaded file will not open. What is wrong?

Usually the string is incomplete, encoded twice, or has the wrong extension. Check the byte count against the size the source reports, look for a message about length or padding, and see if the tool says the data is Base64 again.

Is it safe to decode a Base64 file from an unknown source?

Decoding is safe, because it only produces bytes. Opening the result is the risk. Treat the file like any download: do not run a program you did not expect, and open documents in a viewer you trust.