vue二进制流文件下载Excel

const windowOpen = () => {
  axios({ // 用axios发送get请求
      method: 'get',
      url:import.meta.env.VITE_API_URL + '/system/user/downloadExcelTemplateForTeacher', // 请求地址
      headers: {
        'Authorization': userInfoStore.token
      }
    }).then(res => { // 处理返回的文件流
      const fileUrl = res.request.responseURL;
      axios.get(fileUrl, {
                    responseType: "blob",
                    headers: {
                        Authorization: userInfoStore.token,
                    },
                })
                .then((response) => {
                    const blob = new Blob([response.data], {
                        type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' // 设置Excel文件类型
                    });
                    const url = URL.createObjectURL(blob);
                    const link = document.createElement('a');
                    link.href = url;
                    link.download = "示例文件.xlsx"; // 设置下载文件的名称为.xlsx格式
                    document.body.appendChild(link);
                    link.click();
                    document.body.removeChild(link);
                });
    })
}

文件上传后缀名与文件类型对照表

1、xls
application/vnd.ms-excel
2、xlsx
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
3、ppt
application/vnd.ms-powerpoint
4、pptx
application/vnd.openxmlformats-officedocument.presentationml.presentation
5、doc
application/msword
6、docx
application/vnd.openxmlformats-officedocument.wordprocessingml.document
7、zip
application/x-zip-compressed
8、rar
application/x-zip-compressed
9、wmv
video/x-ms-wmv
10、mp3
audio/mpeg
11、mp4
video/mp4
12、gif
image/gif
13、jpg
image/jpeg
14、png
image/png
15、bmp
image/bmp
16、psd
application/octet-stream
17、ico
image/x-icon
18、7z
application/octet-stream
19、exe
application/octet-stream
20、avi
video/avi
21、rmvb
application/vnd.rn-realmedia-vbr
22、3gp
application/octet-stream
23、flv
application/octet-stream
24、wav
audio/wav
25、krc
application/octet-stream
26、lrc
application/octet-stream
27、txt
text/plain
28、pdf
application/pdf
29、chm
application/octet-stream
30、mdb
application/msaccess
31、sql
application/octet-stream
32、con
application/octet-stream
33、log
text/plain
34、dat
application/octet-stream
35、ini
application/octet-stream
36、php
application/octet-stream
37、html 和 htmhtm
text/html
38、ttf
application/octet-stream
39、fon
application/octet-stream
40、js
application/x-javascript
41、xml
text/xml
42、dll
application/octet-stream

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下载Excel文件是指通过Vue前端框架实现在网页上实时下载Excel文件的功能。为了实现这一功能,可以通过以下步骤: 1. 在Vue组件中使用axios或者其他网络请求库,向后端发送请求获取Excel文件数据。 2. 后端接收到请求后,根据接口要求生成Excel文件,并将文件返回给前端。 3. 前端接收到Excel文件数据后,通过Blob对象和URL.createObjectURL方法创建可供下载Excel文件链接。 4. 在Vue组件中使用a标签或者其他方式,将Excel文件链接绑定到下载按钮或者表格中。用户点击下载按钮即可下载Excel文件。 在编写相关代码时,需要注意文件的处理、请求的发送和响应的处理。具体示例代码如下:(以axios为例) ```javascript // Vue组件中发送请求 <template> <div> <button @click="downloadExcel">下载Excel</button> </div> </template> <script> import axios from 'axios'; export default { methods: { downloadExcel() { axios({ method: 'get', url: 'http://your-backend-api-url/export-excel', responseType: 'blob', // 告诉axios返回的数据是二进制 }) .then(response => { const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement('a'); link.href = url; link.setAttribute('download', 'excel.xlsx'); document.body.appendChild(link); link.click(); }) } } } </script> ``` 以上就是使用Vue下载Excel文件的基本方法。通过这种方式,用户可以在网页上快速方便地下载后端生成的Excel文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值