Uint8Array.fromBase64()
Uint8Array.fromBase64() 静态方法根据 base64 编码的字符串创建一个新的 Uint8Array 对象。
应优先使用此方法而非 Window.atob(),因为它会生成一个字节数组,这比包含原始字节的字符串更容易处理,除非你解码的二进制数据确实是要作为 ASCII 文本使用。如果你已分配了 ArrayBuffer 并想填充它,请改用实例方法 Uint8Array.prototype.setFromBase64()。
语法
Uint8Array.fromBase64(string)
Uint8Array.fromBase64(string, options)
参数
string-
用于编码要转换为
Uint8Array的字节的 base64 字符串。该字符串只能包含 base64 字母表中的字符,包括 A–Z、a–z、0–9,以及两个特殊字符。如果options中使用alphabet: "base64",则为+和/;如果使用alphabet: "base64url",则为-和_。字符串末尾可能包含填充字符=。字符串中的任何 ASCII 空格字符都将被忽略。 options可选-
一个用于自定义 base64 字符串解释过程的对象。它可以包含以下属性:
alphabet可选-
一个指定要使用的 base64 字母表的字符串。它可以是以下之一:
"base64"(默认)-
接受使用标准 base64 字母表编码的输入,该字母表使用
+和/。 "base64url"-
接受使用 URL 安全 base64 字母表编码的输入,该字母表使用
-和_。
lastChunkHandling可选-
一个字符串,指定如何处理 base64 字符串的最后一块。由于 base64 中的每 4 个字符编码 3 个字节,因此字符串会分成 4 个字符一组的块。如果最后一块不足 4 个字符,则需要以不同方式处理。它可以是以下之一:
"loose"(默认)-
最后一块可以是 2 或 3 个 base64 字符,或者正好是 4 个字符长并带有填充字符
=。最后一块将被解码并附加到结果中。 "strict"-
最后一块必须正好是 4 个字符长并带有填充字符
=。此外,溢出位(最后 base64 字符中不代表任何数据的尾随位)必须为 0。最后一块将被解码并附加到结果中。 "stop-before-partial"-
如果最后一块正好是 4 个字符长并带有填充字符
=,则它将被解码并附加到结果中。否则,最后一块不完整的块将被忽略(但如果它包含一个 base64 字符后跟=,则仍会抛出语法错误)。当字符串来自流且最后一块尚未完成时,这很有用。要了解输入读取了多少字符,请改用Uint8Array.prototype.setFromBase64()(链接的页面还包含使用"stop-before-partial"进行流解码的示例)。
返回值
包含从 base64 编码字符串解码出的字节的新 Uint8Array 对象。
异常
SyntaxError-
如果输入字符串包含指定字母表之外的字符,或者最后一块不满足
lastChunkHandling选项,则会抛出此错误。 TypeError-
在以下情况之一中抛出
- 输入字符串不是字符串。
options对象不是对象或undefined。- 选项不是预期的值或为
undefined。
示例
解码 base64 字符串
此示例使用默认的 alphabet 和 lastChunkHandling 选项来解码 base64 字符串。请注意:
- 空格中的空白字符被忽略。
- 该字符串包含 14 个 base64 字符,不是 4 的倍数。这仅在使用
lastChunkHandling: "loose"时才有效且可解码。 - 最后一块
Ph以字符h结尾,其 base64 表示为0b100001,因此最后0001位是“溢出位”并被忽略。这仅在使用lastChunkHandling: "loose"时才有效且可解码。
const uint8Array = Uint8Array.fromBase64("PGI+ TURO PC9i Ph");
console.log(uint8Array); // Uint8Array(10) [60, 98, 62, 77, 68, 78, 60, 47, 98, 62]
解码 URL 安全的 base64 字符串
此示例使用 alphabet 选项来解码 URL 安全的 base64 字符串。
const uint8Array = Uint8Array.fromBase64("PGI-TUROPC9iPg", {
alphabet: "base64url",
});
console.log(uint8Array); // Uint8Array(10) [60, 98, 62, 77, 68, 78, 60, 47, 98, 62]
使用严格的最后一块处理来解码 base64 字符串
此示例使用 lastChunkHandling 选项来解码 base64 字符串,其中最后一块必须正好是 4 个字符长并带有填充字符 =,并且溢出位必须为 0。
const array1 = Uint8Array.fromBase64("PGI+ TURO PC9i Pg==", {
lastChunkHandling: "strict",
});
console.log(array1); // Uint8Array(10) [60, 98, 62, 77, 68, 78, 60, 47, 98, 62]
const array2 = Uint8Array.fromBase64("PGI+ TURO PC9i Ph==", {
lastChunkHandling: "strict",
});
// Throws a SyntaxError because h is 0b100001, where the last 4 bits are not 0
const array3 = Uint8Array.fromBase64("PGI+ TURO PC9i Pg", {
lastChunkHandling: "strict",
});
// Throws a SyntaxError because the last chunk is not exactly 4 characters long
使用部分最后一块处理来解码 base64 字符串
此示例使用 lastChunkHandling 选项来解码 base64 字符串,忽略任何不完整的最后一块。
// The last chunk is complete
const array1 = Uint8Array.fromBase64("PGI+ TURO PC9i", {
lastChunkHandling: "stop-before-partial",
});
console.log(array1); // Uint8Array(9) [60, 98, 62, 77, 68, 78, 60, 47, 98]
// The last chunk is also complete with padding
const array2 = Uint8Array.fromBase64("PGI+ TURO PC9i Pg==", {
lastChunkHandling: "stop-before-partial",
});
console.log(array2); // Uint8Array(10) [60, 98, 62, 77, 68, 78, 60, 47, 98, 62]
// The last chunk is partial; it's ignored
const array3 = Uint8Array.fromBase64("PGI+ TURO PC9i Pg", {
lastChunkHandling: "stop-before-partial",
});
console.log(array3); // Uint8Array(9) [60, 98, 62, 77, 68, 78, 60, 47, 98]
// The last chunk is partial with padding; it's still ignored
const array4 = Uint8Array.fromBase64("PGI+ TURO PC9i Pg=", {
lastChunkHandling: "stop-before-partial",
});
console.log(array4); // Uint8Array(9) [60, 98, 62, 77, 68, 78, 60, 47, 98]
// The last chunk is partial, but it contains one base64 character followed by `=`
const array5 = Uint8Array.fromBase64("PGI+ TURO PC9i P=", {
lastChunkHandling: "stop-before-partial",
});
// Throws a SyntaxError because this cannot possibly be part of a valid base64 string
规范
| 规范 |
|---|
| Uint8Array 与 base64 的相互转换 # sec-uint8array.frombase64 |
浏览器兼容性
加载中…