我想通过特定的路由将一些结果返回给用户。所以我有:
@route('/export')
def export_results():
#here is some data gathering ot the variable
return #here i want to somehow return on-the-fly my variable as a .txt file所以,我知道如何:
但是:我听说我可以以某种特定的方式设置响应http头,将我的变量作为文本返回给浏览器,就像文件一样。
问题是:如何使它以这种方式运行?
P.S.:如我在Python3上的标签所示,使用瓶子并计划将cherrypy中的服务器作为wsgi服务器。
发布于 2016-05-06 18:20:32
如果我正确理解,您希望访问者的浏览器提供将响应保存为文件的功能,而不是将其显示在浏览器本身中。要做到这一点很简单,只需设置以下标题:
Content-Type: text/plain
Content-Disposition: attachment; filename="yourfilename.txt"浏览器将提示用户保存该文件,并建议文件名为"yourfilename.txt“。(更多讨论here.)
若要设置瓶中的标头,请使用response.set_header
from bottle import response
@route('/export')
def export():
the_text = <however you choose to get the text of your response>
response.set_header('Content-Type', 'text/plain')
response.set_header('Content-Disposition', 'attachment; filename="yourfilename.txt"')
return the_texthttps://stackoverflow.com/questions/37074068
复制相似问题