vue2文件流下载

后端采用文件流的方式将文件信息发送给前端,前端需要接收相应的流信息做出对应的操作

1.请求后端接口返回文件流:

            axios({
              url, //请求地址
              method:'get', //请求方式
              responseType:'blob' //请求相应文件流类型
            }).then(res=>{
              let name = ''//根据 type 的值不同 定义不同文件名称
              if(this.type === 1){
                 name = '文件名' 
              }else if(this.type === 2){
                 name = '文件名' 
              }else if(this.type === 3){
                name = '文件名' 
              }
              let type = 'xlsx' //文件类型
              this.downloadZip(res.data,name,type) //调用下载方法
            })

2.处理文件流方法 下载文件

/**
         * 下载压缩包文件
         * @param {blob} fileArrayBuffer 文件流
         * @param {String} filename 文件名称
         * @param {String} fileType 文件格式
         */
        downloadZip (fileArrayBuffer, filename, fileType)  {
            //new Blob([文件流],{文件类型 === 'zip' 吗?  是 : 不是 }) 创建二进制对象,
          let data = new Blob([fileArrayBuffer], { type: fileType == 'zip' ?     'application/zip,charset=utf-8' : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
          if (typeof window.chrome !== 'undefined') {
            // Chrome
            var link = document.createElement('a');
            link.href = window.URL.createObjectURL(data);
            link.download = filename;
            link.click();
            console.log(data);
          } else if (typeof window.navigator.msSaveBlob !== 'undefined') {
            // IE
            var blob = new Blob([data], { type: fileType == 'zip' ? 'application/zip' : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
            window.navigator.msSaveBlob(blob, filename);
          } else {
            // Firefox
            var file = new File([data], filename, { type: fileType == 'zip' ? 'application/zip' : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
            window.open(URL.createObjectURL(file));
          }
        },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3是一种用于构建用户界面的JavaScript框架,文件下载功能通常在Web开发中用于实现文件下载的功能。在Vue3中,我们可以通过使用`axios`库来发送HTTP请求,并处理文件下载。以下是实现Vue3文件下载功能的基本步骤: 1. 首先,我们需要安装`axios`库。可以使用以下命令进行安装: ``` npm install axios ``` 2. 在需要使用文件下载功能的Vue组件中,我们需要引入`axios`库: ```javascript import axios from 'axios'; ``` 3. 接下来,我们可以创建一个方法来处理文件下载的请求。例如,我们可以创建一个名为`downloadFile`的方法: ```javascript methods: { downloadFile() { axios({ url: 'http://example.com/download', method: 'GET', responseType: 'blob', // 指定响应的数据类型为二进制 }) .then((response) => { const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement('a'); link.href = url; link.setAttribute('download', 'filename.ext'); // 指定下载文件名 document.body.appendChild(link); link.click(); }) .catch((error) => { console.error(error); }); } } ``` 4. 最后,在Vue组件的模板中,我们可以使用一个按钮或其他元素来触发文件下载操作。例如,我们可以在模板中添加一个按钮: ```html <button @click="downloadFile">下载文件</button> ``` 以上就是使用Vue3实现文件下载功能的基本步骤。通过使用`axios`库发送HTTP请求,我们可以获取到文件的二进制数据,并通过创建临时URL来实现文件下载。在下载时,我们可以为文件指定名称以及自定义其他下载选项,以满足具体需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值