简体   繁体   English

Python Flask send_from_directory 返回损坏的文件

[英]Python Flask send_from_directory returning corrupted file

We have written code to convert a file from json to excel format and its returning the excel file where as if I try to open the file its corrupted.我们编写了代码来将文件从 json 转换为 excel 格式并返回 excel 文件,就好像我尝试打开已损坏的文件一样。

We have tried pandas and openpyxl to convert the json.我们已经尝试过 pandas 和 openpyxl 来转换 json。 Below is the sample code tried to check whether everything is working fine or not.以下是尝试检查一切是否正常的示例代码。

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.

I was facing the same issue with PPTX files.我在使用 PPTX 文件时遇到了同样的问题。 Used curl for downloading file, and it worked.使用 curl 下载文件,它工作。

The issue is with windows security.问题在于 windows 安全性。

Go to Downloaded file's properties. Go 到下载文件的属性。 There will be unblock checkbox at the end.最后会有取消阻止复选框。 Check it and Press Ok .检查它并按OK Now try to open file again.现在尝试再次打开文件。

You can also check if MD5 of both files match or not.您还可以检查两个文件的MD5是否匹配。

See this for reference 请参阅此以供参考

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

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