前言
axios请求返回的是一张图片,这种情况下,我们可以直接将src定义为请求的地址,但是,如果有请求头等其他信息,这个方法就非常的鸡肋了,所以,我们还是需要搞一下
URL.createObjectURL()
话不多说,我们直接上代码
this.axios({
url:'http://127.0.0.1:8000/sss/',
method:'get',
responseType:'arraybuffer'
}).then(res => {
return 'data:image/png;base64,' + btoa(
new Uint8Array(res)
.reduce((data, byte) => data + String.fromCharCode(byte), '')
);
}).then(data => {
this.image = data //图片地址
})
要注意,respnseType是必须的,如果不加,会出不来
我在这里自己定义了一个image,src属性绑定的就是他,这样,我们就获取到了文件流的同时,把图片预览出来。
- Post link: https://www.godhearing.cn/axios-huo-qu-wen-jian-liu-bing-qie-zhan-shi-wei-tu-pian/
- Copyright Notice: All articles in this blog are licensed under unless otherwise stated.