首页 > 解决方案 > 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 文件。

标签: pythonodooodoo-8

解决方案


文件内容表明它是 .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 文件的正确内容类型是什么?


推荐阅读