1.template直接加
<a
:href="`${$store.state.fileUrl}${item.filePath}`"
:download="item.fileOldName"
>
{{ item.fileOldName }}
<i
class="el-icon-download"
style="float: right;line-height:30px"
/>
</a>
2.在methods创建方法
writeUploadFile(val) {
const fileName = val.fileOldName;
const linkNode = document.createElement('a');
linkNode.style.display = 'none';
linkNode.href = `${this.$store.state.fileUrl}${val.filePath}`;
linkNode.download = fileName;
document.body.appendChild(linkNode);
linkNode.click();
URL.revokeObjectURL(linkNode.href);
document.body.removeChild(linkNode);
},
3.后台接口返回blob流
此时请求接口 responseType: ‘blob’,
如有文件损坏 responseType: ‘arraybuffer’,
async writeLogs() {
const writeLogs = await writeOperationLogList();
if (writeLogs) {
const blob = new Blob([writeLogs], { type: 'application/vnd.ms-excel' });
const fileName = '操作日志.xlsx';
if ('download' in document.createElement('a')) {
const linkNode = document.createElement('a');
linkNode.style.display = 'none';
linkNode.href = URL.createObjectURL(blob);
linkNode.download = fileName;
document.body.appendChild(linkNode);
linkNode.click();
URL.revokeObjectURL(linkNode.href);
document.body.removeChild(linkNode);
} else {
navigator.msSaveBlob(blob, fileName);
}
}
},