ArrayBuffer和base64字符串互相转化

辰漪
2022-02-08 / 0 评论 / 25 阅读 / 正在检测是否收录...

1. ArrayBuffer转base64

function translateArrayBufferToBase64(buffer){
  const bytes = new Uint8Array(buffer).reduce((data, byte) => data + String.fromCharCode(byte), '')
  return window.btoa(bytes);
}

2. base64转ArrayBuffer

function translateBase64ToArrayBuffer(base64){    
  const binaryStr = window.atob(base64);
  const byteLength = binaryStr.length;
  const bytes = new Uint8Array(byteLength);
  for(let i=0;i<byteLength;i++){
      bytes[i] = binaryStr.charCodeAt(i);
  }
  return bytes.buffer;
}
1
选择打赏方式:
微信

评论 (0)

取消