python - Flask:使用 GET 请求下载 .zip 文件
问题描述
烧瓶 v1.1.2,Python v3.7
我试图让 Flask 向我发送它根据 GET 请求生成的 .zip 文件。
我有一个页面,用户可以在其中单击下载按钮,该按钮发出 GET 请求并将键解析为值。基于此密钥创建 .zip 文件并将其移至我的/static
文件夹。在被执行的处理程序下方:
def download(self):
key = request.args['action']
zip_location = os.path.join(app.static_folder, "files.zip")
res = self.recorder.down.download(key, zip_location)
return send_file("static/files.zip", as_attachment=True)
我尝试使用send_file
, send_from_directory
,发送文件app.send_static_file
,但它们都导致发生相同的奇怪错误:
Serving Flask app "webapp.webUI" (lazy loading)
Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
Debug mode: on
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Debugging middleware caught exception in streamed response at a point where response headers were already sent.
Traceback (most recent call last):
File "/home/pi/somepath/env/lib/python3.7/site-packages/werkzeug/wsgi.py", line 506, in __next__
return self._next()
File "/home/pi/somepath/env/lib/python3.7/site-packages/werkzeug/wrappers/base_response.py", line 45, in _iter_encoded
for item in iterable:
TypeError: 'Response' object is not iterable
127.0.0.1 - - [18/Feb/2021 13:34:58] "GET /download?action=20210216+16u11s19 HTTP/1.1" 200 -
谷歌并没有太大帮助,我一直在寻找错误的原因几个小时。
解决方案
尝试
from flask import Flask, request
# set the project root directory as the static folder, you can set others.
app = Flask(__name__, static_url_path='')
@app.route('/download')
def download():
return app.send_static_file('files.zip')
推荐阅读
- mysql - 遇到错误 #1060 - 列“forum_enabled”重复
- java - com.google.android.gms.ads.internal.util.aq.a 中的 NoClassDefFoundError 出现奇怪错误
- swift - 为什么不满足这个说法?
- java - Java BufferedFileReader 跳过第一行
- git - Subgit导入没有标准布局的根目录
- django - 部署到heroku时不断发生django博客错误
- xaml - 如何在 XAML 中的 Rectangle BoxView 内放置文本
- ruby-on-rails - 搜索方法 Ruby
- python - 在 Seaborn 的散点图中可视化与预期值的距离
- c# - 如何设置允许 Lambda 相互通信和 RDS 的 VPC?