vue总结_下载流文件

vue项目中接受后台传过来的流文件会出现乱码

后台response的内容如下:

在参考各位大大资料的基础下,做了点总结。

具体处理方法如下

1、方法一:通过插件https://github.com/kennethjiang/js-file-download

      在需要使用接受文件的地方

//  需要安装cnpm install js-file-download --save     var fileDownload = require("js-file-download");
然后在接口部分
// 导出文件流
  fetchExportBill(url, data = {}) {
    return new Promise((resolve, reject) => {
      axios.post(url,data,{ responseType: 'arraybuffer'}).then(res => {
      //resolve(res)
      //以下fileName是取后台的文件名,如果没有'content-disposition',可以直接略过这一步,自己定:如fileName="xxx.xlsx"。
     let fileName = res.headers['content-disposition'].match(/fushun(\S*)xls/)[0];
    fileDownload(res.data,fileName);
      }).catch(error => {
        if (data.Vue) {
          data.Vue.$message.error('系统异常');
        }
        reject(null, e);
      })
    })
  },

2、方法二:通过Blob实现

// 导出文件流
  fetchExportBill(url, data = {}) {
    return new Promise((resolve, reject) => {
      axios.post(url,data,{ responseType: 'arraybuffer'}).then(res => {
      //resolve(res)
      let blob = new Blob([res], {type: "application/vnd.ms-excel"}); 
  let objectUrl = URL.createObjectURL(blob); 
   window.location.href = objectUrl; 
      }).catch(error => {
        if (data.Vue) {
          data.Vue.$message.error('系统异常');
        }
        reject(null, e);
      })
    })
  },

 注:Blob对象的type为MIME类型,具体参考 vue总结_MIME

        Blob具体使用方式参考 :MDNBlo 


Filedownload
     详细 X
网络释义
Filedownload: 资料下载

 

 

blob   [blɒb]   blob&type=1 详细 X
基本翻译
n. 一滴;一抹;难以名状的一团
vt. 弄脏;把…做错
vi. 得零分;弄错
网络释义
BLOB: 二进制大对象
blob volume: 油滴体积
Key BLOB: 密钥块

 

 

转载于:https://www.cnblogs.com/yjmBlogs/p/9493470.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值