请求URL后,vuejs和axios无法下载文件.xls

时间:2019-06-03 22:27:20

标签: vue.js axios

当我需要使用axios和vuejs下载.xls文件时遇到问题。

我的代码是在用户按下按钮之后,它将使用具有授权头的API调用API,然后返回带有附件文件(.xls)的API。

这是我的下载功能

download(url, data = null, options = {}) {

        let vuex = JSON.parse(localStorage.getItem('vuex'))
        let accessToken = vuex.Auth.accessToken
        let header = {
            Authorization: `Bearer ${accessToken}`
        }
        return http
            .post(url, data, {
                ...options,
                headers: {
                    ...header,
                    ...options.headers
                },
                responseType: 'arraybuffer',
            })
            .then(res => res.data)
            .then(data => {
                let blob = new Blob([data], { type: 'application/vnd.ms-excel' })
                let link = document.createElement('a')
                link.href = window.URL.createObjectURL(blob)
                link.download = 'Report.xls'
                link.click()

            })
    },

它返回一些数据,如... enter image description here

但是我无法下载文件,因为我得到了'Access-Control-Allow-Origin' header is present on the requested resource

我的代码有什么问题。

0 个答案:

没有答案
相关问题