vue 用axios实现调用接口下载excel(一定要指定类型为二进制流)

 


import axios from 'axios'
exportExcel () {
        let url = 'http...',
        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://www.cnblogs.com/shun1015/p/12580815.html