django - 尝试使用 Django-Rest-Framework 下载 excel 文件时获取 unicode 字符作为响应
问题描述
我正在开发应用程序,我需要在使用 react 单击下载按钮时下载保存在项目媒体目录中的 excel 文件。我已经为它编写了一个 api,从我找到的答案中使用 django rest 框架下载文件,但邮递员给了我一个 unicode 字符的响应。
这是我的 DRF 观点:
def download(self):
file_name= 'reports.xlsx'
file_path = (settings.MEDIA_ROOT +'/'+ file_name).replace('\\', '/')
file_wrapper = FileWrapper(open(file_path,'rb'))
file_mimetype = mimetypes.guess_type(file_path)
response = HttpResponse(file_wrapper, content_type=file_mimetype )
response['X-Sendfile'] = file_path
response['Content-Length'] = os.stat(file_path).st_size
response['Content-Disposition'] = 'attachment; filename=%s' % str(file_name)
return response
如果有人可以帮助我解决它,我将不胜感激。谢谢!
解决方案
推荐阅读
- django - 如何在 github/ 中获得合作
- .net - 如何让后台工作人员 DoWork() 等待进一步执行,直到在 ProgressChange() 事件中完成处理?
- reactjs - 是否可以在页面加载时使用默认值加载剑道反应表单
- highcharts - 如何更改 highcharts gatt 中列单元格的背景颜色?
- jquery - 在每次点击时为 TinyMCE 添加引号而不是重置
- javascript - 添加多次图像下拉屏幕(视频游戏js)
- django - 使用 django 渠道进行电子商务的电子购物车(不使用 ajax)
- java - 为什么我的 jsf 托管 bean 中的列表为空
- reactjs - SwiperSlide onTouchStart/onClick => 触发 slideNext()
- python - 通过 localhost:5000 访问时标记不起作用