Reputation: 59405
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?
Upvotes: 1
Views: 1540
Reputation: 910
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
Upvotes: 1
Reputation: 7875
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.
Upvotes: 3
Reputation: 1331
Add 'Content-Disposition: attachment; filename="<file>"'
header to response
Upvotes: 2