Odoo 使响应文件下载损坏
Odoo make response file download corrupted
我正在尝试在 Odoo 8 中下载响应文件。下面的代码适用于 Linux。但在 Windows OS 中文件下载已损坏。
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)。所以内容类型应该是
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
(参见 What is correct content-type for excel files?)
发生此问题是因为 Python 在 Windows 和 Linux 上的行为不同。在 windows 上,打开文件模式应该是 rb
而不仅仅是 r
。
我正在尝试在 Odoo 8 中下载响应文件。下面的代码适用于 Linux。但在 Windows OS 中文件下载已损坏。
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)。所以内容类型应该是
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
(参见 What is correct content-type for excel files?)
发生此问题是因为 Python 在 Windows 和 Linux 上的行为不同。在 windows 上,打开文件模式应该是 rb
而不仅仅是 r
。