我正在尝试在Odoo8中下载响应文件。下面的代码在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))])
文件下载内容如下所示
^{pr2}$奥多本身没有报告任何错误。为什么会这样?有解决办法吗?还有为什么文件是excel时zip文件头呢?在
PS.I确认文件路径存在,文件不是zip文件,而是excel文件。在
这个问题的发生是因为Python在Windows和Linux上的行为不同。在windows上,打开文件模式应该是
rb
,而不仅仅是r
。在文件内容表明它是一个.xlsx文件,而不是.xls(PK是ZIP存档的签名,.xlsx文件是XML文件的ZIP文件,如这里所述https://en.wikipedia.org/wiki/Microsoft_Excel#File_formats)。所以内容类型应该是
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
(见What is correct content-type for excel files?)
相关问题 更多 >
编程相关推荐