前端处理文件流
使用场景: 后端以文件流的方式返回数据,前端需要进行转化后使用
图片流文件
第一步、配置获取图片流接口
export function getCurrent(dev){
return api({
url:'/xxx',
method:'get',
responseType:'blob',
params:{dev}
})
}
第二步、设置图片显示区域
<img :src="currentPicture" alt="">
第三步、文件流转化地址
init(){
getCurrent(this.routename).then(res => {
let blob = new Blob([res.data],{type:'image/jpeg'})
this.currentPicture = window.URL.createObjectURL(blob)
})
}
转化后的地址长这样:blob:http://localhost:8080/7083d583-a30e-45f9-ac56-5674e93da8c8
excel流
步骤和上面类似主要需要将type
改成对应的文件类型
如excel文件就是 type: "application/vnd.ms-excel",
然后就是用对应的标签展示
let a = document.createElement("a")
let blob = new Blob([res], {
type: "application/vnd.ms-excel", //将会被放入到blob中的数组内容的MIME类型
})
let objectUrl = URL.createObjectURL(blob) //生成一个url
a.setAttribute("href", objectUrl)
a.setAttribute("download", "盘点模板.xls")
a.click()