后端返回Excel导出文件流前端实现下载
后端只返回文件流,没有其他文件名之类的情况下:
XML/HTML Code复制内容到剪贴板
- <el-button size="large" type="primary" @click="onClickExport">导出记录</el-button>
JavaScript Code复制内容到剪贴板
- const onClickExport = () => {
- $API.user.settlement.exportRecord.post({},{responseType: "blob"}).then(res => {
- // data就是接口返回的文件流
- const data = res
- const fileName = '打款记录-' + $TOOL.dayjs.now() + '.xlsx'
- let blob = new Blob([data], { type: "application/vnd.ms-excel" });
- let fileUrl = URL.createObjectURL(blob);
- let a = document.createElement("a");
- a.setAttribute("href", fileUrl);
- a.setAttribute("download", fileName);
- a.click();
- }).catch(err => {
- }).finally(() => {
- })
- }