1.新建导出文件接口
封装上传文件接口
export function Export(data){
return axios.post('/api/export',data,{
headers:{
responseType:"blob"
}
})
}
2.引用调取接口
import {Export} from '@/utils/request'
Export({ }).then(res=>{
const BLOB = res; //创建一个Blob对象
const fileReader = new FileReader(); //fileReader 对象允许Web应用程序异步读取存储在用户计算机上的文件的内容
fileReader.readAsDataURL(BLOB) //开始读取指定的Blob对象中的内容,一旦完成,result属性中将包含一个data:URL格式的Base6
//字符串以表示所读文件的内容
fileReader.onload = (event)=>{
//处理load事件。该事件再读取操作完成时触发
//新建一个下载的a标签,完成后移除
let a = document.createElement('a')
a.download = ''
a.href = event.target.result
document.body.appendChild('a')
a.click()
document.body.removeChild('a')
}
})