簡體   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