Stuart Robertson
Stuart Robertson

Reputation: 793

web.py downloading images

I'm fairly new to web applications and I'm trying to write a web.py server that will allow me to download an image. As of right now I'm keeping it down to the bare minimum until I can see how it all works. I have this bit of code that will return me an image when I go the correct URL:

class download:

   def GET(self, args):
       path = 'path/to/image' 
       web.header('Content-type','images/jpeg')
       web.header('Content-transfer-encoding','binary') 
       return open(path, 'rb').read()

So when I go to the URL it automatically downloads the image however, it names it as 'download' and has no extension. Is there a way to specify the file name for when it get's downloaded? Does this have to be specified in the headers somewhere?

Upvotes: 1

Views: 1021

Answers (2)

freestyler
freestyler

Reputation: 5444

You need set the Content-Disposition header, for example:

web.header('Content-Disposition', 'attachment; filename="fname.ext"')

reference: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.5.1

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599956

You want the content-disposition header:

'Content-Disposition: attachment; filename=my_filename.jpg'

Upvotes: 2

Related Questions