首页 > 移动平台 > 详细

vue 用axios实现调用接口下载excel

时间:2019-03-06 00:01:04      阅读:344      评论:0      收藏:0      [点我收藏+]

了解的方式有两种:

1. 用a标签,href设置为后端提供的excel接口

<a href="excel接口">导出</a>

简单方便,缺点就是当有token校验时,不适合

2. 用axios

把token放在请求的header里边

import axios from axios
import { getToken } from js-cookie;

methods: {
    exportExcel () {
        let url = http...,
            token = getToken();
        axios.get(url, {
            headers:{
                "Admin_token":token
            },
            responseType: blob, //二进制流
        }).then(function (res) {
            if(!res) return
            let blob = new Blob([res.data], {type: application/vnd.ms-excel;charset=utf-8})
            let url = window.URL.createObjectURL(blob);
            let aLink = document.createElement("a");
            aLink.style.display = "none";
            aLink.href = url;
            aLink.setAttribute("download", "excel.xls");
            document.body.appendChild(aLink);
            aLink.click();
            document.body.removeChild(aLink); 
            window.URL.revokeObjectURL(url); 
        }).catch(function (error) {
            console.log(error)
        });
    }
}

 

 

 

参考:https://blog.csdn.net/xuesheng1610748/article/details/83865679

vue 用axios实现调用接口下载excel

原文:https://www.cnblogs.com/bear-blogs/p/10480478.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!