美文网首页
下载excel文件流

下载excel文件流

作者: robotlee8 | 来源:发表于2018-09-09 23:32 被阅读12次

需求是按查询条件导出


WechatIMG3.png

后端提供的接口是get方式,并且有入参


WechatIMG1.png
接口返回的是文档流
WechatIMG2.png

最简单的方式是利用a标签href直接打开拼接后的get地址。
也可以用window.open()打开拼接好的url,但是后重新打开一个tab页,虽然是瞬间的但是体验不好
经过谷歌查询以后采用了以下方式

axios.get(`/insuranceOrder/exportExcel`, { //url: 接口地址
                    params: this.queryParam,
                    responseType: `arraybuffer` //一定要写

                })
                .then(res => {
                    if (res.status == 200) {
                        let blob = new Blob([res.data], {
                            type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
                            //word文档为application/msword,pdf文档为application/pdf,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8
                        });
                        let objectUrl = URL.createObjectURL(blob);
                        let link = document.createElement("a");
                        let fname = `保险询价列表`; //下载文件的名字
                        link.href = objectUrl;
                        link.setAttribute("download", fname);
                        document.body.appendChild(link);
                        link.click();
                    } else {
                        this.$message({
                            type: "error",
                            message: "导出失败"
                        })
                    }
                })

相关文章

网友评论

      本文标题:下载excel文件流

      本文链接:https://www.haomeiwen.com/subject/uzcjgftx.html