vue如何通过$http的post方法请求下载二进制的Excel文件

方式一:单独接口,接口封装 特定的service.js

1.下载

//下载
export function getReportTemplate(){
    return new Promise((resolve, reject) => {
        axios({
            method: 'post',
            url: '/fas/engine/web/fund/final/template',
            responseType: "blob",
            headers: {
                'Content-Type': 'application/json;charset=UTF-8',
            },
        }).then((res) => {
            resolve(res)
        }).catch(error => {
            reject(error)
        });
    })
}

使用此function

//下载上报的模板
uploadTemplate(){
    getReportTemplate().then((res) => {
        console.log("getReportTemplate",res);
        var blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'})
        var downloadElement = document.createElement('a')//创建一个a 虚拟标签
        var href = window.URL.createObjectURL(blob) // 创建下载的链接
        downloadElement.href = href;
        downloadElement.download = "基金预算编报模板"; // 下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href) // 释放掉blob对象
    });
},

2.上传

//上传
export function getReport(formData){
    return new Promise((resolve, reject) => {
        axios({
            method: 'post',
            url: '/fas/engine/web/fund/budget/addReport',
            data: formData,
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        }).then((res) => {
            resolve(res)
        }).catch(error => {
            reject(error)
        });
    })
}

使用此function, test.vue