我有一个基于 Flask 的网站,用户可以在其中下载一些 PDF 文件。
使用 Flask 可以直接实现send_file() http://flask.pocoo.org/docs/0.10/api/#flask.send_file and send_from_directory() http://flask.pocoo.org/docs/0.10/api/#flask.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 http://flask.pocoo.org/docs/0.10/api/#flask.after_this_request钩。但看起来像send_file()
异步运行所以@after_this_request
可能会在文件下载之前触发。
- 例如,如果文件很大,下载可能需要一段时间,因此
@after_this_request
似乎在下载文件时触发。
- 它看起来来自文档 http://flask.pocoo.org/docs/0.10/api/#flask.send_file like
send_file()
使用 WSGI 的文件包装器来完成下载...也许这就是它异步运行的原因?
有办法打电话吗after_download()
这样就可以保证在之后运行send_file()
has 完全的将文件发送给用户?
你不能。尽管send_file
在使用开发服务器时使用 Flask 流式传输文件,它可能(并且确实应该为了性能)使用 Web 服务器(nginx、apache 等)并且X-SendFile
在生产中。由于 Web 服务器超出了应用程序的控制范围,因此您运气不好。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)