python - Odoo 使响应文件下载损坏
问题描述
我正在尝试在 Odoo 8 中下载响应文件。下面的代码在 Linux 上运行。但在 Windows 操作系统中,文件下载已损坏。
filecontent = None
with open("C:\\report_media\\output\\" + output, "r") as f:
if f.mode == 'r':
_logger.debug('File object %s', f)
filecontent = f.read()
if not filecontent:
return http.request.not_found()
else:
return http.request.make_response(filecontent,
[("Content-Type", "application/vnd.ms-excel"),
("Content-Disposition", content_disposition(output))])
文件下载内容长这样
PK g‘M#ÏÀ _rels/.rels’O‹Â@Å¿J™û
Odoo 本身不报告任何错误。为什么会这样?有解决办法吗?另外为什么文件是excel时zip文件头?
PS。我确认文件路径存在,并且该文件不是 zip 文件,它是一个 excel 文件。
解决方案
文件内容表明它是 .xlsx 文件,而不是 .xls(PK 是 ZIP 存档的签名,而 .xlsx 文件是 XML 文件的 zip,如此处所述https://en.wikipedia.org /wiki/Microsoft_Excel#File_formats)。所以 Content-Type 应该是
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
(请参阅Excel 文件的正确内容类型是什么?)
推荐阅读
- node.js - mongoose 查询中的多个人口,如果一个失败,有没有办法获得其余的填充数据
- angular - 使用 Jest 测试包含“declare var”的组件或服务
- dc.js - 如何从 dc.js 中的任何维度按自定义值过滤记录?
- react-native - 解决 App Store 在 React Native Expo 上的 UIWebView 弃用警告?
- javascript - 使用 JS 和 Dropwdown 按 ID 选择器切换显示/隐藏 DIV
- java - 当我打印列表时,LinkedList 得到了相同的值,即使我输入了不同的值也很困难
- sql - 试图拉出一定数量的客户购买清单,数据库似乎完全忽略了我的减号
- alexa - Alexa Remainder API (REST API) - 无效的不记名令牌
- python - python中的RESTful API请求[张量流服务]
- firebase - 从 Firebase Functions 上传后,为什么我的图片没有加载到存储仪表板中?