如图,实现类似这样的点击直接下载附件。给按钮绑定@click="handleDownload(fileUrl, fileName)"
/**
* @description 下载
* @param {String} fileUrl 附件地址
* @param {String} fileName 附件名称
*/
handleDownload(fileUrl = '', fileName = '默认文件名称') {
// 点击直接下载
var a = document.createElement('a')
fetch(fileUrl)
.then(res => res.blob())
.then(blob => {
a.href = URL.createObjectURL(blob)
a.download = fileName
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
})
},