主要针对后台返回的文件流进行处理
/**
* 文件流转换 主要代码块,可自定义下载文件名称
* @param {} data
*/
export function download (data,titName) {
if(!data){
return
}
const content = data
const blob = new Blob([content],{type: "application/vnd.ms-excel"})
const fileName = titName?titName: '报废卡号.xlsx'
if ('download' in document.createElement('a')) { // 非IE下载
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
} else { // IE10+下载
navigator.msSaveBlob(blob, fileName)
}
}
引用
一定要加 responseType: ‘blob’。
// 引入方法
import { download } from '../../../api/apiStock'
// 数据请求
getDownload(){
this.$axios({
url:'接口地址(get参数最好拼接在url上)',
methods:'get',
responseType: 'blob',
}).then(res=>{
download(res.data,'卡号卡密.xlsx')
})
}
//给按钮绑定click 事件 = getDownload()即可