Guoguo-notes
主页
常用笔记
vue笔记及周边生态
  • 团队协作及规范
  • 项目框架及架构
  • 飞码篇
  • Java
  • React笔记
GitHub
主页
常用笔记
vue笔记及周边生态
  • 团队协作及规范
  • 项目框架及架构
  • 飞码篇
  • Java
  • React笔记
GitHub
  • vue笔记及周边生态

    • 1. Element Plus --vue3.0.md
    • 2. Element ui 笔记.md
    • 3. Taro.md
    • 4. Vue 代码片段.md
    • 5. Vue 全局封装 main.js.md
    • 6. Vue 笔记.md
    • 7. Vue3 .md
    • 8. Vue3+Element Plus.md
    • 9. element plus 本地启动.md
    • 10. jsx tsx 代码片段.md
    • 11. jsx tsx 笔记.md
    • 12. uniapp笔记.md
    • 13. vite 笔记.md
    • 14. vite手写插件.md
    • 15. vue.js 下载文件.md
    • 16. vueuse笔记.md
    • 17. vxe-table笔记.md
    • 18. 代码片段 - 解析数学公式.md
    • 19. 常用插件.md
    • 20. 汇智腾远笔记.md
    • 21. 视频播放插件.md

下载

下载blob文件流

前端下载后台返回的文件流 ,诸如以下:

g����^� 6�p��U���r΀%�좃����/�I�|�Rˤ��:f����~ ��mF�v����ׯ������p9HB�SyݵK~�����PK �;��

const binaryData = []
binaryData.push(res.data)

// 指定文件编码方式为 utf-8
const blob = new Blob(binaryData, { type: 'application/vnd.ms-excel;charset=utf-8' })

// 获取blob链接
this.Url = window.URL.createObjectURL(blob)
window.open(this.Url)

下载XML文件

import axios from 'axios'

axios({
    url: '/dev-api/standarInterface/archives/' + row.id,
    method: 'GET',
    responseType: 'blob', // important
}).then((response) => {
    const url = window.URL.createObjectURL(new Blob([response]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', 'file.xml');
    document.body.appendChild(link);
    link.click();
});

下载excel

一定要加这个不然会乱码 responseType:'blob',

// 导出文件
export function exportTemplate(params) {
    return request({
        url: '/statistics/completion/exportTemplate',
        method: 'get',
        responseType:'blob',
        params
    })
}

   const url = window.URL.createObjectURL(new Blob([res]));
      const link = document.createElement('a');
      link.href = url;
      link.setAttribute('download', '非投资与建设项目招标完成情况表.xlsx');
      document.body.appendChild(link);
      link.click();

或者使用这种方式

const blob = new Blob([res.data], { type: 'application/vnd.ms-excel;charset=utf-8' });

// 获取临时URL
this.Url = window.URL.createObjectURL(blob);

// 下载文件
const link = document.createElement('a');
link.href = this.Url;
link.download = 'data.xlsx';
link.click();

// 清理临时资源
window.URL.revokeObjectURL(this.Url);

导出PDF

const res = await exportPdf({ settleNumber: this.resultData.settleNumber })
// 获取blob链接
const blob = new Blob([res], { type: 'application/pdf;charset=UTF-8' });
// 获取临时URL
this.Url = window.URL.createObjectURL(blob);
// 下载文件
const link = document.createElement('a');
link.href = this.Url;
link.download = '结算单商品明细(' + this.resultData.inTime + ').xlsx';
link.click();
// 清理临时资源
window.URL.revokeObjectURL(this.Url);
Edit this page
Last Updated:
Contributors: 袁果锅
Prev
14. vite手写插件.md
Next
16. vueuse笔记.md