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 个回答
2
你需要使用 content-disposition
这个头信息:
'Content-Disposition: attachment; filename=my_filename.jpg'
2
你需要设置一个叫做 Content-Disposition
的头部信息,比如:
web.header('Content-Disposition', 'attachment; filename="fname.ext"')
参考链接: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.5.1