小程序arraybuffer转成base64
时间: 2023-09-22 10:05:57 浏览: 205
image-to-base64-or-buffer:压缩图像(jpg,png),然后将其编码为base64
可以使用以下代码将ArrayBuffer转换为Base64:
```javascript
function arrayBufferToBase64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
```
调用示例:
```javascript
var buffer = new Uint8Array([72, 101, 108, 108, 111, 33]).buffer;
var base64 = arrayBufferToBase64(buffer);
console.log(base64); // "SGVsbG8h"
```
希望能对您有所帮助。还有什么可以为您服务的吗?
阅读全文