通过Axios从Django下载文件



我正在尝试使用Axios和Django从blob请求中获取文件名。

Django的观点:

with open(full_file_path, 'rb') as f:   
response = HttpResponse(f, content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')  
file_expr = "filename*=utf-8''{}".format(quote(file_name_with_ext))  
response['Content-Disposition'] = 'attachment; {}'.format(file_expr)  
return response

前端:

axios
.request({
url,
method,
responseType: 'blob',
})
.then(({ data }) => {
console.log(data);
const downloadUrl = window.URL.createObjectURL(new Blob([data]));
const link = document.createElement('a');
link.href = downloadUrl;
link.setAttribute('download', 'file.xlsx');
document.body.appendChild(link);
link.click();
link.remove();
});

但参数"data"不包含允许我获取文件名的标头。

有什么解决办法吗?

代替此.then(({ data }) => {使用此:.then(response =>{

response.data是您可以通过console.log(response.data);看到的来自响应的数据

希望它能帮助<3.

最新更新