0

I'm trying to make response file download in Odoo 8. The code below worked on Linux. But in Windows OS the file download corrupted.

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))])

The file download content looks like this

PK    g‘M#ÏÀ        _rels/.rels­’O‹Â@Å¿J™û

Odoo itself doesn't report any error. Why this is happening? Is there is a fix for this? Also why the zip file header when the file is excel?

PS. I confirm the file path existed, and the file is not zip file, it is an excel file.

strike_noir
  • 4,080
  • 11
  • 57
  • 100
  • Have you tried debugging the code? What is the result of `filecontent` on Windows? Is the path in the `open` call correct, also on Windows? Does it use \ instead of / ? Is the drive letter correct? Is there a different working directory on Windows than on Linux (depends on your start script)? – miw Aug 18 '18 at 16:13
  • I have tried with "\" its the same result. I have updated the code using "with" – strike_noir Aug 24 '18 at 10:56

2 Answers2

0

The file content indicates that it's an .xlsx file, not an .xls (the PK is the signature of a ZIP archive, and an .xlsx file is a zip of XML files, as described here https://en.wikipedia.org/wiki/Microsoft_Excel#File_formats). So the Content-Type should be application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

(see What is correct content-type for excel files?)

memo
  • 1,868
  • 11
  • 19
0

This issue happened because different behavior between Python on Windows and Linux. On windows the open file mode should have been rb not just r.

strike_noir
  • 4,080
  • 11
  • 57
  • 100