- 单个附件下载
javascript
<el-button
type="text"
size="small"
@click.native.prevent="download(scope.row)" >下载</el-button>
export default {
data() {
return {
downloadUrl: 'http://127.0.0.1:8881/XX/XX', // 下载接口
}
},
methods: {
download(row) { // 单个附件下载
// 下载附件(form方式)
const params = {
fileId: row.dataId,
fileName: row.fileName,
attachmentId: row.dataId,
token: getToken()
}
this.postDownloadFile(params, this.downloadUrl)
// downloadFileByToken({ attachmentId: row.dataId }).then((response) => { // 按钮提交无效
// })
},
postDownloadFile(params, url) {
const form = document.createElement('form')
form.style.display = 'none'
form.action = url
form.method = 'post'
document.body.appendChild(form)
// 动态创建input并给value赋值
for (const key in params) {
const input = document.createElement('input')
input.type = 'hidden'
input.name = key
input.value = params[key]
form.appendChild(input)
}
form.submit()
form.remove()
},
},
}
灵感来自:https://blog.csdn.net/ewl0116/article/details/129879216