接口 在请求接口时 要带上responseType:'blob'
export function downloadData (params) {
return Service({
// baseURL: ConfigBaseURL,
responseType: 'blob',
headers: { 'Content-Type': 'application/json; application/octet-stream' },
url: '/approval/downloadData',
method: 'get',
params
})
}
方法
downloadEvent(row){
downloadData({id: row.id}).then(res => {
if(row.saveType === 0){
// 获取数据类型
const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
const a = document.createElement('a')
// 创建URL
const blobUrl = window.URL.createObjectURL(blob)
a.download = '申请数据.xlsx'
a.href = blobUrl
document.body.appendChild(a)
// 下载文件
a.click()
// 释放内存
URL.revokeObjectURL(blobUrl)
document.body.removeChild(a)
}else if(row.saveType === 1){
// 获取数据类型
const blob = new Blob([res.data], { type: 'application/zip' })
const a = document.createElement('a')
// 创建URL
const blobUrl = window.URL.createObjectURL(blob)
a.download = '申请数据.zip'
a.href = blobUrl
document.body.appendChild(a)
// 下载文件
a.click()
// 释放内存
URL.revokeObjectURL(blobUrl)
document.body.removeChild(a)
}
})
},