前端处理后台返回的乱码图片

问题描述:

  请求后台接口,返回如下图的乱码。

根据全场乱字符+JFIF标记,确定是个JPEG文件的二进制流,而不是base64。

解决方法:

  

1 this.$axios.get('api',{responseType: 'arraybuffer'})
2  .then(res=>{
3     this.formData.imgUrl =  'data:image/png;base64,' + btoa(
4     new Uint8Array(res.data).reduce((data, byte) => data + String.fromCharCode(byte), ''))
5 })

注意:*****设置请求数据类型:responseType:'arraybuffer'******

猜你喜欢

转载自www.cnblogs.com/sansgun/p/9367554.html