【问题标题】:web.py downloading imagesweb.py 下载图片
【发布时间】:2012-12-31 21:36:05
【问题描述】:

我对 Web 应用程序还很陌生,我正在尝试编写一个 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 时,它会自动下载图像,但将其命名为“下载”并且没有扩展名。有没有办法指定下载时的文件名?是否必须在某个地方的标题中指定?

【问题讨论】:

    标签: python http web.py


    【解决方案1】:

    你想要content-disposition 标头:

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

    【讨论】:

    • 添加行:web.header('Content-disposition', 'attachment; filename=file.jpg') 有效。谢谢!
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 2019-01-31
      • 2016-02-25
      • 2010-11-23
      • 2013-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多