- 点击下载按钮,调接口,这时候接口的返回值为res,返回的是一个文件,可以这样对文件进行处理:
//接口中定义responseType:'arraybuffer',observe:'response'
this.loadFile(res,name)//调用封装的下载方法
//下载方法
loadFile(res,filename){
const blob =new Blob([res],{
type:'application/vnd.ms-excel'
})
if('download' in document.createElement('a')){
const link =document.createElement('a')
link.download=filename
link.style.display='none'
link.href=URL.createObjectURL(blob)
document.body.appendChild(link)
link.click()
URL.revokeObjectURL(link.href)
document.body.removeChild(link)
}else{
navigator.msSaveBlob(blob)
}
}