【问题标题】:Python Flask send_from_directory returning corrupted filePython Flask send_from_directory 返回损坏的文件
【发布时间】:2021-05-11 18:41:36
【问题描述】:

我们编写了将文件从 json 格式转换为 excel 格式的代码,并返回了 excel 文件,就好像我试图打开它损坏的文件一样。

我们已经尝试过 pandas 和 openpyxl 来转换 json。 以下是尝试检查一切是否正常的示例代码。

from flask import request, send_from_directory
df1 = pd.DataFrame([['a', 'b'], ['c', 'd']], index=['row 1', 'row 2'], columns=['col 1', 'col 2'])
df1.to_excel('/tmp/output.xlsx')
return send_from_directory('/tmp/', 'output' + '.xlsx', as_attachment=True)```

Notes: The code has been deployed using kubernetes, we have copied the generated file from the volume and its looking good, while transferring its getting corrupted. And we have tried different content types as well.

【问题讨论】:

    标签: pandas flask openpyxl sendfile


    【解决方案1】:

    我在使用 PPTX 文件时遇到了同样的问题。使用 curl 下载文件,效果很好。

    问题在于 Windows 安全性。

    转到已下载文件的属性。最后会有unblock复选框。检查它并按确定。现在尝试再次打开文件。

    您还可以检查两个文件的 MD5 是否匹配。

    See this for reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 2020-06-23
      • 1970-01-01
      相关资源
      最近更新 更多