二进制转base64

一. 以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 字符串,
其中字符串中的每个字符都被视为一个二进制数据字节。

 

转载于:https://www.cnblogs.com/cai-yu-candice/p/9562306.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值