This page was translated from English by the community.Learn more and join the MDN Web Docs community.
Кодирование и декодирование в формате Base64
Base64 - это группа схожихbinary-to-text encoding схем, которые представляют двоичные данные в ASCII-формате методом перевода в radix-64 представление. ТерминBase64 происходит от a specificMIME content transfer encoding.
Кодирование Base64 широко используется в случаях, когда требуется перекодировать двоичные данные для передачи по каналу приспособленному для передачи текстовых данных. Это делается с целью защиты двоичных данных от любых возможных повреждений при передаче. Base64 широко используется во многих приложениях, включая электронную почту (MIME), и при сохранении больших объёмов данных вXML.
В языке JavaScript существуют две функции, для кодирования и декодирования данных в/из формат Base64 соответственно:
Функция atob() декодирует Base64-кодированную строку. В противоположность ей, функцияbtoa() создаёт Base64 кодированную ASCII строку из "строки" бинарных данных.
Обе функцииatob() иbtoa() работают со строками. Если вам необходимо работать сArrayBuffers, обратитесь к этому параграфу.
In this article
Документация
dataURIsdataURIs, описанные вRFC 2397, позволяют создателям контента встроить в документ маленькие файлы в виде строки (инлайном).- Base64
Wikipedia article about Base64 encoding.
atob()Decodes a string of data which has been encoded using base-64 encoding.
btoa()Creates a base-64 encoded ASCII string from a "string" of binary data.
- The "Unicode Problem"
In most browsers, calling
btoa()on a Unicode string will cause aCharacter Out Of Rangeexception. This paragraph shows some solutions.- URIScheme
List of Mozilla supported URI schemes
StringViewIn this article is published a library of ours whose aims are:
- creating aC-like interface for strings (i.e. array of characters codes —
ArrayBufferViewin JavaScript) based upon the JavaScriptArrayBufferinterface, - creating a collection of methods for such string-like objects (since now:
stringViews) which workstrictly on array of numbers rather than on immutable JavaScript strings, - working with other Unicode encodings, different from default JavaScript's UTF-16
DOMStrings,
- creating aC-like interface for strings (i.e. array of characters codes —
Tools
Related Topics
The "Unicode Problem"
SinceDOMStrings are 16-bit-encoded strings, in most browsers callingwindow.btoa on a Unicode string will cause aCharacter Out Of Range exception if a character exceeds the range of a 8-bit byte (0x00~0xFF). There are two possible methods to solve this problem:
- the first one is to escape the whole string (with UTF-8, see
encodeURIComponent) and then encode it; - the second one is to convert the UTF-16
DOMStringto an UTF-8 array of characters and then encode it.
Here are the two possible methods.
Solution #1 – escaping the string before encoding it
function b64EncodeUnicode(str) { // first we use encodeURIComponent to get percent-encoded UTF-8, // then we convert the percent encodings into raw bytes which // can be fed into btoa. return btoa( encodeURIComponent(str).replace( /%([0-9A-F]{2})/g, function toSolidBytes(match, p1) { return String.fromCharCode("0x" + p1); }, ), );}b64EncodeUnicode("✓ à la mode"); // "4pyTIMOgIGxhIG1vZGU="b64EncodeUnicode("\n"); // "Cg=="To decode the Base64-encoded value back into a String:
function b64DecodeUnicode(str) { // Going backwards: from bytestream, to percent-encoding, to original string. return decodeURIComponent( atob(str) .split("") .map(function (c) { return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2); }) .join(""), );}b64DecodeUnicode("4pyTIMOgIGxhIG1vZGU="); // "✓ à la mode"b64DecodeUnicode("Cg=="); // "\n"Unibabel implements common conversions using this strategy.
Solution #2 – rewrite the DOMsatob() andbtoa() using JavaScript'sTypedArrays and UTF-8
Use aTextEncoder polyfill such asTextEncoding (also includes legacy windows, mac, and ISO encodings),TextEncoderLite, combined with aBuffer and a Base64 implementation such asbase64-js.
When a nativeTextEncoder implementation is not available, the most light-weight solution would be to useTextEncoderLite withbase64-js. Use the browser implementation when you can.
The following function implements such a strategy. It assumes base64-js imported as<script type="text/javascript" src="base64js.min.js"/>. Note that TextEncoderLite only works with UTF-8.
function Base64Encode(str, encoding = "utf-8") { var bytes = new (TextEncoder || TextEncoderLite)(encoding).encode(str); return base64js.fromByteArray(bytes);}function Base64Decode(str, encoding = "utf-8") { var bytes = base64js.toByteArray(str); return new (TextDecoder || TextDecoderLite)(encoding).decode(bytes);}