博客
关于我
vue使用axios接收后台返回的文件流下载文件
阅读量:509 次
发布时间:2019-03-07

本文共 766 字,大约阅读时间需要 2 分钟。

后台接口返回的数据

代码部分:

this.axios({    method: "get",    headers: {      "content-type": "application/json", // 默认值      Authorization: "Bearer " + sessionStorage.getItem("access_token"),    },    url: 'your_URL',    params: {'name':'Jack'},    responseType: "blob",  })    .then(function (res) {      let blob = new Blob([res.data]); // { type: "application/vnd.ms-excel" }      let url = window.URL.createObjectURL(blob); // 创建一个临时的url指向blob对象      // 创建url之后可以模拟对此文件对象的一系列操作,例如:预览、下载      let a = document.createElement("a");      a.href = url;      a.download = "表格.xlsx";      a.click();      // 释放这个临时的对象url      window.URL.revokeObjectURL(url);    })    .catch(function (res) {      console.log("error", res);    });

注意:  responseType: "blob"必须写 不然下载下来的excel是损坏文件打不开。

转载地址:http://nuwnz.baihongyu.com/

你可能感兴趣的文章
Netty 异步任务调度与异步线程池
查看>>
Netty 的 Handler 链调用机制
查看>>
Netty 编解码器和 Handler 调用机制
查看>>
Netty 编解码器详解
查看>>
Netty 解决TCP粘包/半包使用
查看>>
Netty 调用,效率这么低还用啥?
查看>>
Netty 高性能架构设计
查看>>
Netty+Protostuff实现单机压测秒级接收35万个对象实践经验分享
查看>>
Netty+SpringBoot+FastDFS+Html5实现聊天App详解(一)
查看>>
netty--helloword程序
查看>>
netty2---服务端和客户端
查看>>
【Flink】Flink 2023 Flink易用性和稳定性在Shopee的优化-视频笔记
查看>>
Netty5.x 和3.x、4.x的区别及注意事项(官方翻译)
查看>>
netty——bytebuf的创建、内存分配与池化、组成、扩容规则、写入读取、内存回收、零拷贝
查看>>
netty——Channl的常用方法、ChannelFuture、CloseFuture
查看>>
netty——EventLoop概念、处理普通任务定时任务、处理io事件、EventLoopGroup
查看>>
netty——Future和Promise的使用 线程间的通信
查看>>
netty——Handler和pipeline
查看>>
Vue输出HTML
查看>>
netty——黏包半包的解决方案、滑动窗口的概念
查看>>