File to Base64
File to Base64 / data URI · Base64 back to a file · automatic image previews
Base64 是怎么膨胀三分之一的
Base64 把每 3 个字节(24 位)切成 4 个 6 位,每个 6 位查表落成一个可打印字符(A–Z、a–z、0–9、+、/),于是二进制变成能塞进 JSON、XML、邮件与 HTML 属性的纯文本。字节数不是 3 的倍数时用 = 补位:余 1 字节补 ==、余 2 字节补 =。所以编码后长度恒为 4 × ⌈字节数 ÷ 3⌉——本页显示的膨胀率是实测字符数 ÷ 原始字节数,并与这个理论值逐字节核对,对不上会明确标出来。
Base64 不是加密,也不是压缩。它只是换字母表:任何人拿到都能一步还原,体积还涨约 1/3。敏感内容请用真正的加密(AES 等),需要变小请用压缩——两件事都不是本页在做的。
data URI 是「前缀 + Base64」。形如 data:image/png;base64,iVBOR…:data: 后面是 MIME 与可选参数(如 ;charset=utf-8),;base64 声明编码方式,逗号之后才是内容。把图片直接写进 HTML 的 src、把字体写进 CSS 用的就是它——代价是体积膨胀且无法被单独缓存。
大文件为什么分块读。编码按约 1MB 一块顺序读取,块长取 3 的整数倍(这样每块都能独立编码、直接拼接,不会在块边界多出补位符),每块编码完立即释放字节。结果是内存里只多出 Base64 文本本身;但文本仍要完整放进页面(约 1.33 倍),所以超过 20MB 请改用命令行工具。
FAQ
Nothing is uploaded. Reading, encoding and decoding all happen in your browser - not a byte of the file leaves your machine, reloading clears it, and nothing is written to localStorage. But understand that Base64 is an encoding, not encryption: it swaps binary for plain text so it fits into JSON, email or HTML - anyone who gets it can reverse it in one step. For secrecy use real encryption like AES. It also inflates size by about a third, so do not treat it as compression for large transfers.
20MB per file. Encoding reads in roughly 1MB chunks and releases each right after, so the file is not copied wholesale into memory several times; but the Base64 text itself must sit in the page complete (about 1.33x volume), plus browser memory limits - for bigger files use the command line: on Linux and macOS base64 file > out.txt, on Windows PowerShell use the one-line Convert call on the file bytes. With multiple files selected they are processed one by one, each row in the list showing its status.
Three clues, in priority order: the MIME in a data URI (data:image/png;base64,... states it outright); the binary signature at the start (PNG is 89 50 4E 47, JPEG is FF D8 FF, PDF starts %PDF, ZIP starts PK); and if the content reads like text it is offered as .txt. When none of the three decides, the file is named .bin with an explicit note that the type could not be inferred from content - the extension is only a suggestion for the download name, the bytes are untouched, and renaming to the right extension opens it normally.
The encoding-conversion page serves small text snippets (paste text to Base64, URL-safe variant, strip padding) plus quick single-file copying; this page serves files: multi-select batches, chunked reading of big files, raw size and inflation ratio, plain Base64 versus data URI forms, and decoding Base64 back into a downloadable file with type detection and image preview. Converting a piece of text - use encoding conversion; handling files - especially images, PDFs or archives above tens of KB - use this page.