繁体   English   中英

Python Flask send_from_directory 返回损坏的文件

[英]Python Flask send_from_directory returning corrupted file

我们编写了代码来将文件从 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.

我在使用 PPTX 文件时遇到了同样的问题。 使用 curl 下载文件,它工作。

问题在于 windows 安全性。

Go 到下载文件的属性。 最后会有取消阻止复选框。 检查它并按OK 现在尝试再次打开文件。

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

请参阅此以供参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM