一. 以fetch的獲取數據
1. response(后台返回): const buffer = response.arrayBuffer(),將二級制轉成arrayBuffer類型
2. buffer轉成base64
function arrayBufferToBase64 = buffer => {
let binary = '';
const bytes = new Uint8Array(buffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i += 1) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary); //base64
};
3. url = `data:image/png;base64,${你的數據流}`
同事提供的建議,親測有效
相關資源:
ArrayBuffer 對象用來表示通用的、固定長度的原始二進制數據緩沖區。
ArrayBuffer 不能直接操作,而是要通過類型數組對象或 DataView 對象來操作,
它們會將緩沖區中的數據表示為特定的格式,並通過這些格式來讀寫緩沖區的內容。
The Uint8Array typed array represents an array of 8-bit unsigned integers.
The contents are initialized to 0. Once established,
you can reference elements in the array using the object's methods,
or using standard array index syntax (that is, using bracket notation).
fromCharCode() 可接受一個指定的 Unicode 值,然后返回一個字符串。
btoa() 從 String 對象中創建一個 base-64 編碼的 ASCII 字符串,
其中字符串中的每個字符都被視為一個二進制數據字節。