Uint8Array.fromBase64()

基準線 2025
新推出

自 ⁨2025 年 9 月⁩起,此功能適用於最新裝置和瀏覽器版本。此功能可能不適用於較舊的裝置或瀏覽器。

Uint8Array.fromBase64() 靜態方法根據 base64 編碼的字串建立一個新的 Uint8Array 物件。

應優先使用此方法而非 Window.atob(),因為它會生成一個位元組陣列,這比包含原始位元組的字串更容易處理,除非你解碼的二進位制資料確實是要作為 ASCII 文字使用。如果你已分配了 ArrayBuffer 並想填充它,請改用例項方法 Uint8Array.prototype.setFromBase64()

語法

js
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 字串

此示例使用預設的 alphabetlastChunkHandling 選項來解碼 base64 字串。請注意:

  • 空格中的空白字元被忽略。
  • 該字串包含 14 個 base64 字元,不是 4 的倍數。這僅在使用 lastChunkHandling: "loose" 時才有效且可解碼。
  • 最後一塊 Ph 以字元 h 結尾,其 base64 表示為 0b100001,因此最後 0001 位是“溢位位”並被忽略。這僅在使用 lastChunkHandling: "loose" 時才有效且可解碼。
js
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 字串。

js
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。

js
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 字串,忽略任何不完整的最後一塊。

js
// 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

瀏覽器相容性

另見