rob*_*jpg 14 html download flask
我有一个页面,显示目录中的文件列表.当用户单击"下载"按钮时,所有这些文件都压缩为单个文件,然后提供下载.我知道如何在单击按钮时将此文件发送到浏览器,并且我知道如何重新加载当前页面(或重定向到另一个页面),但是可以在同一步骤中执行这两个操作吗?或者通过下载链接重定向到不同的页面会更有意义吗?
我的下载是使用Flask API启动的send_from_directory.相关测试代码:
@app.route('/download', methods=['GET','POST'])
def download():
error=None
# ...
if request.method == 'POST':
if download_list == None or len(download_list) < 1:
error = 'No files to download'
else:
timestamp = dt.now().strftime('%Y%m%d:%H%M%S')
zfname = 'reports-' + str(timestamp) + '.zip'
zf = zipfile.ZipFile(downloaddir + zfname, 'a')
for f in download_list:
zf.write(downloaddir + f, f)
zf.close()
# TODO: remove zipped files, move zip to archive
return send_from_directory(downloaddir, zfname, as_attachment=True)
return render_template('download.html', error=error, download_list=download_list)
Run Code Online (Sandbox Code Playgroud)
更新:作为一种解决方法,我现在正在加载一个按钮单击的新页面,这使用户可以send_from_directory在返回更新的列表之前启动下载(使用).
您是否在前端Web服务器(如nginx或apache)后面运行烧瓶应用程序(这将是处理文件下载的最佳方式).如果您使用的是nginx,则可以使用"X-Accel-Redirect"标题.对于这个例子,我将使用该目录/srv/static/reports作为您正在创建zipfile的目录,并希望将它们提供给它们.
nginx.conf
在该server部分
server {
# add this to your current server config
location /reports/ {
internal;
root /srv/static;
}
}
Run Code Online (Sandbox Code Playgroud)
你的烧瓶方法
将标头发送到nginx到服务器
from flask import make_response
@app.route('/download', methods=['GET','POST'])
def download():
error=None
# ..
if request.method == 'POST':
if download_list == None or len(download_list) < 1:
error = 'No files to download'
return render_template('download.html', error=error, download_list=download_list)
else:
timestamp = dt.now().strftime('%Y%m%d:%H%M%S')
zfname = 'reports-' + str(timestamp) + '.zip'
zf = zipfile.ZipFile(downloaddir + zfname, 'a')
for f in download_list:
zf.write(downloaddir + f, f)
zf.close()
# TODO: remove zipped files, move zip to archive
# tell nginx to server the file and where to find it
response = make_response()
response.headers['Cache-Control'] = 'no-cache'
response.headers['Content-Type'] = 'application/zip'
response.headers['X-Accel-Redirect'] = '/reports/' + zf.filename
return response
Run Code Online (Sandbox Code Playgroud)
如果你正在使用apache,你可以使用他们的sendfile指令http://httpd.apache.org/docs/2.0/mod/core.html#enablesendfile
| 归档时间: |
|
| 查看次数: |
9251 次 |
| 最近记录: |