python - 烧瓶 send_file() 发送空文本文档
问题描述
我正在从数据库中加载一些数据并将其插入到文本文件中,但作为概念证明,我正在使用虚拟数据,但我似乎无法下载包含任何内容的文本文件。
@app.route("/results", methods=["GET"])
def results():
if request.method == "GET":
with open("output.txt", 'w') as file:
file.write("Test 1 2 3")
return send_file("output.txt", as_attachment=True, cache_timeout=0)
文本文件下载但它是空的?但是,查看创建文件的目录,其中有文本!?
就好像文件在完成写入之前正在下载?
解决方案
打开文件以二进制模式读取对我有用。
尝试将最后一行从
return send_file("output.txt", as_attachment=True, cache_timeout=0)
至
file_to_be_sent = open("output.txt", 'rb')
return send_file(file_to_be_sent, as_attachment=True, cache_timeout=0)
推荐阅读
- multiboot - 从其他主机控制操作系统选择菜单
- python - 为什么我的二叉树的最大深度解决方案返回的比预期的少一?
- reactjs - 部署到 AWS s3 webpack 时出错
- c++ - 使用 C++ 进行库管理
- amazon-web-services - AWS 控制塔将账户移动到新的 OU 失败
- reactjs - 在 Reactjs 中通过 ASP.NET Core (Api) jwt 使用外部登录
- node.js - 在 Google Cloud Console 界面中应用环境变量
- reactjs - 如何在函数组件中使用构造函数(胖箭头函数)?
- c++ - 如何在 Windows 10 相机应用程序中执行实时人脸检测?
- javascript - 用于选择查找中的所有选项的复选框