web.py下载图片

时间:2013-01-17 10:56:31

标签: python http web.py

我是网络应用程序的新手,我正在尝试编写一个允许我下载图像的web.py服务器。到目前为止,我将它保持在最低限度,直到我能看到它是如何工作的。我有一些代码,当我输入正确的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()

因此,当我转到URL时,它会自动下载图像,但是它将其命名为“download”并且没有扩展名。有没有办法指定下载时的文件名?这是否必须在某处的标题中指定?

2 个答案:

答案 0 :(得分:1)

您需要content-disposition标题:

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

答案 1 :(得分:1)

您需要设置Content-Disposition标头,例如:

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

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

相关问题