以下是个人项目中下载文件常用的两种方式:
1. 前端通过a标签下载:
<a target="_blank" :download="scope.row.name" :href="scope.row.url" :name="scope.row.name" >下载</a>
2. 前端通过fetch下载:
<el-button type="text" size="small" @click="downloadFile(scope.row.url, scope.row.name)">下载</el-button>
async downloadFile(url, name){
let response = await fetch(url)
// 内容转变成blob地址
let blob = await response.blob()
// 创建隐藏的可下载链接
let objectUrl = window.URL.createObjectURL(blob)
let a = document.createElement('a')
a.style.display = "none";
//地址
a.href = objectUrl
//修改文件名
a.download = name
// 触发点击
document.body.appendChild(a)
a.click()
//移除
URL.revokeObjectURL(a.href);
},