原因:跨域后系统为了安全去掉自定义头
解决方法:
django后台
后端在返回response
时,加上header Access-Control-Expose-Headers
如下:response['Content-Disposition'] ='' #自定义头
response['Access-Control-Expose-Headers'] = "Content-Disposition" #Content-Disposition为自定义头的key
response = HttpResponse(content=result, content_type="application/octet-stream", charset="utf-8")
response['Content-Disposition'] = 'attachment; filename='+reports_name # 自定义响应头
response['Access-Control-Expose-Headers'] = "Content-Disposition" # 通过此header允许前端访问 解决跨域后系统为了安全去掉自定义头
return response
VUE前台
// 下载文件
downloadReport(e) {
this.$axios({
method: "post",
url: this.$path + "tests/batteryReportDownload/",
data: {
}
}).then(res => {
let headers = res.headers;
let fileName = headers["content-disposition"]; #获取自定义头
});
},