// 更通用的post方法导出excel文件
import axios from 'axios'
export function excelExport(data, queryUrl, name) {
    // const data = this.searchForm
    let url = process.env.VUE_APP_BASE_API_BACK_END_URL + process.env.VUE_APP_BASE_API_BACK_END
    axios({
        method: 'post',
        url: url + queryUrl, // 请求地址
        data: data, // 参数
        responseType: 'blob',
    }).then(res => {
        let newName = name
        // 处理返回的文件流
        const content = res.data

        const blob = new Blob([content], { type: 'application/octet-stream;charset=ISO8859-1' })
        console.log(res.headers)
        // const fileName = res.headers['content-disposition'].split('filename=')[1]
        const fileName = newName
        if ('download' in document.createElement('a')) {
            // 非IE下载
            const elink = document.createElement('a')
            elink.download = fileName
            elink.style.display = 'none'
            elink.href = URL.createObjectURL(blob)
            document.body.appendChild(elink)
            elink.click()
            URL.revokeObjectURL(elink.href) // 释放URL 对象
            document.body.removeChild(elink)
        } else {
            // IE10+下载
            navigator.msSaveBlob(blob, fileName)
        }
    })
}

 

posted on 2020-08-17 18:51  去吃饭了  阅读(942)  评论(0编辑  收藏  举报