遇到个项目 有token鉴权,所有下载都不能直接给链接,学习后然后就有了以下的
// 下载设备管理excel 模板
export const personTemplate = (params, type, URL) => {
return Axios.get(URL, { responseType: 'arraybuffer' }
).then(res => {
if (type === 1 && params !== null) {
var fileName = params + '模板.xlsx'
} else if (type === 2 && params === null) {
fileName = '列表.xlsx'
}
let blob = new Blob([res.data], { type: 'application/x-xls' })
if (navigator.appVersion.toString().indexOf('.NET') > 0) {
window.navigator.msSaveBlob(blob, fileName)
} else {
let link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = fileName
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
return res.data
})
}
记录一下以后备用
同事提醒优化后
export function personTemplate (data) {
return Axios({
url: '/scp-mdmapp/user/downloadUsers',
method: 'get',
params: data,
responseType: 'arraybuffer'
})
}
调用
/**
* @description 导出按钮
*/
onDownload: function () {
let param = {}
param.houseUuid = this.exportHouseNode.houseUuid
param.orgUuid = this.exportHouseNode.orgUuid
param.name = this.personSearch.name
param.userType = this.personSearch.userType
param.sex = this.personSearch.sex
personTemplate(param)
.then(res => {
let fileName = '人员列表.xlsx'
let blob = new Blob([res.data], { type: 'application/x-xls' })
if (navigator.appVersion.toString().indexOf('.NET') > 0) {
window.navigator.msSaveBlob(blob, fileName)
} else {
let link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = fileName
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
})
.catch(err => {
this.$message({
message: '服务器连接异常',
type: 'warning'
})
console.warn(LOG_TAG + '人员导出下载err')
console.warn(err)
})
}