因为后台请求都得在header里面加token,普通的跳转url header加不上token会提示没有权限无法下载,
后来找见一篇通过ajax下载文件转成blob对象本地封装的办法,但是每次打开都会提示
代码如下
```
downloadWord(row.id)
.then((res) => { // 处理返回的文件流
const content = res.data
let temp = res.headers["content-disposition"]
let isExcel = temp.indexOf(".xlsx") > 0 || temp.indexOf(".xls") > 0
let isWord = temp.indexOf(".docx") > 0 || temp.indexOf(".doc") > 0
let file = {}
if(isExcel){
file.type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
file.name = temp.indexOf(".xlsx") > 0 ? 'xlsx' : 'xls'
}else if(isWord){
file.name = temp.indexOf(".docx") > 0 ? 'docx' : 'doc'
file.type = 'application/msword;charset=UTF-8'
}
// const blob = new Blob([content], {type: res.data.type})
const blob = new Blob([content], {type: file.type})
const fileName = `${row.title}.${file.name}`
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)
}
})
```