2011-09-13 46 views
1

我想使用CherryPy做一个简单的网站,以前从来没有做过Python web编程。如何允许在CherryPy中下载动态创建的文件?

我卡试图允许动态创建的文件的下载。我可以创建一个文件并从处理程序返回它,或者在文件上调用serve_fileobj(),但无论在哪种情况下,文件的内容都会简单地呈现到屏幕上,而不是下载。

CherryPy在这里提供任何有用的方法吗?这如何实现?

回答

2

添加'Content-Disposition: attachment; filename="<file>"'头响应

+0

听起来不错,我会今晚或明天上午测试,并接受它是否工作。 –

+2

serve_fileobj(fileobj,content_type = None,disposition = None,name = None):正确指定desposition,name和content_type – varela

3

如果设置正确的内容类型,你会不会当您返回它除非是合适担心它在浏览器中呈现。在返回内容之前,请尝试:

response.headers['Content-Type'] = 'application/foo' 

(或任何正确的MIME类型的内容)。

1

从@varela和@JasonFruit把以前的答案在一起:

dynamic_content = "this was generated on the fly!" 
cherrypy.response.headers['Content-Disposition'] = 'attachment; filename="<file>"' 
return dynamic_content 
相关问题