ant design pro 导出 Excel文件 Django返回文件流
发布日期:2021-05-27 02:33:34 浏览次数:4 分类:技术文章

本文共 1026 字,大约阅读时间需要 3 分钟。

最近做文件下载发现,ant design pro 的request发送post/get请求都不能将Django返回的FileResponse对象正确解析了,比如如果直接从浏览器上输入服务器的URL,就能直接下载下来文件,但是如果通过ant design Pro 发送get请求,返回的却是一堆乱码,不是一个文件

解决方法是:

Django服务器代码如下:

完整代码:

ant design Pro

function request(url,body_json,name) {   fetch(url, {      method: 'POST',      body: body_json,   })      .then(res => res.blob())      .then(data => {         let blobUrl = window.URL.createObjectURL(data);         download(blobUrl,name);      });}function download(blobUrl,name) {   const a = document.createElement('a');   a.style.display = 'none';   a.download = name;   a.href = blobUrl;   const body = document.getElementsByTagName("body")   body[0].appendChild(a);   a.click();   document.body.removeChild(a);}export default request;

Django代码

file = open(settings.MEDIA_ROOT + '/tempExcel/成绩.xlsx', 'rb')response = FileResponse(file)filename = '成绩'response['Content-Type'] = 'application/vnd.ms-excel;charset=UTF-8'response['Content-Disposition'] = 'attachment;filename="' + urlquote(filename) + '.xlsx"'return response

 

转载地址:https://blog.csdn.net/jjw_zyfx/article/details/98500488 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:js 对null,引号,空格进行判空返回true
下一篇:ant design pro anchor 点击图片就能跳转到指定位置

发表评论

最新留言

关注你微信了!
[***.104.42.241]2023年11月04日 12时21分32秒