前端下载后台传来的流文件
1.window.open(url,’_blank’)
2.使用Blob对象
downfile() {
axios
.post(postUrl, params, {
responseType: 'arraybuffer' })
.then((res) => {
const link = document.createElement('a')
let blob = new Blob([res.data], {
type: 'application/vnd.ms-excel' })
let href = window.URL.createObjectURL(blob) // 创建下载URL
link.href = href
link.download = 'ces.xlsx' // 自定义下载后文件名
link.click() // 点击下载文件
document.body.removeChild(downloadElement) //下载完成移除元素
window.URL.revokeObjectURL(href) // 释放掉blob对象
})
},
3.使用 js-file-download
下载
npm install js-file-download --save
示例
import fileDownload from 'js-file-download'
downfile(){
axios.post(postUrl, params, {
responseType: 'arraybuffer'}).then(res => {
fileDownload(res.data, 'ces.xlsx')
});
}