vue项目 axios接受文件流 显示图片到前端

143 阅读1分钟

请求回来的流有可能是乱码,如图所示

处理方法:

第一步:发送请求时:

let url = `/frm/v1/api/images/${options.id}`;
    axios.get(url,{responseType: 'arraybuffer'}).then(function(data){
      success(data);
    }).catch(function(err){
      error(err)
    });

第二步:处理返回的数据结果

·pic.imgUrl = 'data:image/png;base64,' + btoa(new Uint8Array(data.data).reduce((data, byte) => data + String.fromCharCode(byte), ''))·

第三步:在img标签中

< img v-if="selectedLogoPicture" ref="selectlogoPictureImg" :src="selectedLogoPicture.imgUrl" class="logoImg">