在前后端分离项目中,前端项目获取不到后端项目添加的请求头信息

在一篇关于前后端分离项目的博客中,作者描述了一个问题,即前端在尝试下载文件时,由于无法正确获取到后端设置的请求头Content-Disposition,导致下载失败。后端使用`response.setHeader(Content-Dispositionattachment;filename=+fileName+.xlsx);`来设置文件名,而前端在Download.js中尝试通过`res.headers['content-disposition']`获取该信息。问题的解决方案是后端添加`response.addHeader(Content-Dispositionattachment;filename=+fileName+.xlsx);`和`response.setHeader(Access-Control-Expose-HeadersContent-Disposition);`,允许跨域访问请求头。修复后,前端能成功下载文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前端获取不到后端添加的请求头信息

问题场景

在前后端分离项目中 , 后端添加请求头信息,头存放文件下载名称以及日期等信息,在前端执行下载事件时,发现封装Download.js报错,原因在于头content-disposition获取失败。

发现问题

后端设置请求头信息:

response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

在这里插入图片描述

前端下载事件
                <a-button icon="export" :loading="exporting" @click="() => {
                    this.exporting = true;
                    Download(this.axios.get(this.$Api.*.*.exportCostFinance, {
                        responseType: 'arraybuffer',
                        params: {
                            company: this.form.company === 0 ? 0 : this.form.company || -1,
                            department: this.form.department || 0,
                            branchOffice: this.form.branchOffice || 0,
                            startMonth: this.form.startMonth && this.form.startMonth.format('YYYY/MM/01') || '',
                            endMonth: this.form.endMonth && this.form.endMonth.endOf('month').format('YYYY/MM/DD') || '',
                        }
                    })).finally(() => this.exporting = false);
                }">导出</a-button>
封装Download.js
function Download(request) {
    return new Promise((resolve, reject) => {
        request.then(res => {
            console.log(res)
            let filename = res.headers['content-disposition'].replace('attachment;filename=', '');
            let conentType = res.headers['content-type'];
            const blob = new Blob([res.data], {type: conentType});
            if (typeof window.navigator.msSaveBlob !== 'undefined') {
                // 兼容IE,window.navigator.msSaveBlob:以本地方式保存文件
                window.navigator.msSaveBlob(blob, decodeURI(filename))
            } else {
                // 创建新的URL并指向File对象或者Blob对象的地址
                const blobURL = window.URL.createObjectURL(blob)
                // 创建a标签,用于跳转至下载链接
                const tempLink = document.createElement('a')
                tempLink.style.display = 'none'
                tempLink.href = blobURL
                tempLink.setAttribute('download', decodeURI(filename))
                // 兼容:某些浏览器不支持HTML5的download属性
                if (typeof tempLink.download === 'undefined') {
                    tempLink.setAttribute('target', '_blank')
                }
                // 挂载a标签
                document.body.appendChild(tempLink)
                tempLink.click()
                document.body.removeChild(tempLink)
                // 释放blob URL地址
                window.URL.revokeObjectURL(blobURL)
            }
            resolve(res);
        }).catch(res => {
            reject(res);
        });
    });
}

export {
    Download
}
点击下载事件,content-disposition头信息实际上没有,Download.js报错,下载失败。

在这里插入图片描述

解决问题

 response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
 
 response.setHeader("Access-Control-Expose-Headers","Content-Disposition");

在这里插入图片描述

重启项目,再次点击下载事件,下载成功。

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeDevMaster

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值