【发布时间】:2015-05-25 08:58:01
【问题描述】:
我有一个基于 Flask 的网站,用户可以在其中下载一些 PDF 文件。
使用 Flask 的 send_file() 和 send_from_directory() 很容易实现。
例如:
@app.route('/downloadreport')
def download_report():
return send_from_directory(
'/reports',
'my_report.pdf',
as_attachment=True)
我想执行一些逻辑(我们称之为after_download())下载完成后。
我试过使用@after_this_request 钩子。但看起来send_file() 是异步运行的,所以@after_this_request 可能会在文件下载之前触发。
- 例如,如果文件非常大,下载可能需要一段时间,因此在下载文件时
@after_this_request似乎会触发。 - 从the documentation 看来,
send_file()使用 WSGI 的文件包装器来实现下载...也许这就是它异步运行的原因?
有没有办法调用after_download() 以保证在send_file() 完成将文件发送给用户之后运行?
【问题讨论】:
标签: python download flask werkzeug