什么是文件流格式?如图
1、在封装接口的时候设置responseType: ‘blob’
return axios({
url: `/web/download/excel/${roundsId}`,
method: 'post',
data,
responseType: 'blob'
})
2、调用接口部分
exportAchievement(this.roundsId, data).then(res => {
this.downFile(res, '学生成绩列表')
}).catch(err => {
console.log(err)
})
downFile(content, name) {
const blob = new Blob([content], {
type: 'application/vnd.ms-excel'
})
const fileName = name + '.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)
}
}