ArrayBuffer转为base64
时间: 2023-07-06 07:09:06 浏览: 147
二进制ArrayBuffer图片转化为base64.md
5星 · 资源好评率100%
可以使用以下代码将 ArrayBuffer 转换为 base64 编码:
```javascript
function arrayBufferToBase64(buffer) {
let binary = '';
const bytes = new Uint8Array(buffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
```
使用示例:
```javascript
const myArrayBuffer = new ArrayBuffer(8);
const base64String = arrayBufferToBase64(myArrayBuffer);
console.log(base64String);
```
阅读全文