以下是个人项目中下载文件常用的两种方式:
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) |
| |
| 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); |
| }, |
复制