开发者

How to allow dynamically created file to be downloaded in CherryPy?

I'm trying to use CherryPy for a simple website, having never done Python web programming before.

I'm stuck trying to allow the download of a file that is dynamically created. I can create a file and return开发者_如何学编程 it from the handler, or call serve_fileobj() on the file, but in either case the contents of the file are simply rendered to the screen, rather than downloaded.

Does CherryPy offer any useful methods here? How can this be accomplished?


If you set the correct content type, you won't have to worry about it rendering in the browser when you return it unless it's appropriate. Try:

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

(or whatever the correct MIME type for your content is) before you return the content.


Add 'Content-Disposition: attachment; filename="<file>"' header to response


Putting the previous answers from @varela and @JasonFruit together:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜