ArrayBuffer和base64字符串互相转化

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;
}
© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 共3条

请登录后发表评论