views:

54

answers:

2

I have a python web-application running inside the Google App Engine.

The application creates on user-demand a string and I want the string to be send to the browser client (application/octet-stream?) as a file.

How can i realize this?

A: 

Setting a content-disposition: attachment header will cause most browsers to download whatever you send them as a file. Safari sometimes ignores it.

eliah
But how can I set the header?
Neverland
+3  A: 

If you're using App Engine's own, simple webapp framework, the simplest approach is to have as the get method of your request handler object something like:

def get(self):
  thestring = 'helloworld'  # or however else it's synthesized;-)
  self.response.headers.add_header(
      'content-disposition', 'attachment', filename='hw.txt')
  self.response.out.write(thestring)

Of course, you can use more sophisticated approaches, if you have other different goals, but this meets your goal as stated.

Alex Martelli
You forgot to set the content-type. :)
Nick Johnson