› httpbin - 协议调试工具
› httpstatuses - 协议状态码查询
› httpie - cURL-like tool for humans
Fiddler
Livid
406.95D
547.21D
V2EX  ›  HTTP

关于让 .mp4 / .wmv 文件在浏览器被下载,而不是直接播放

  •  3
     
  •   Livid ·
    PRO
    · Jan 18, 2015 · 12809 views
    This topic created in 4321 days ago, the information mentioned may be changed or developed.

    最近研究了一下这个问题,以前以为只需要添加 Content-Type: application/octet-stream 就可以了,但是发现在 Firefox 和 IE 里还是会被播放,Chrome 和 Safari 里倒是直接下载了。然后 Google 了一下之后发现需要添加的是两个 header:

    Content-Type: application/octet-stream
    Content-Disposition: attachment
    

    这样的话,在所有浏览器里都是启动下载对话框,而不是直接播放。

    关于 Content-Disposition 的更多信息:

    http://www.iana.org/assignments/cont-disp/cont-disp.xhtml

    9 replies  •  2015-01-18 11:28:46 +08:00
    imn1
    imn1
    Jan 18, 2015
    1
    Content-Disposition: attachment
    这句比较重要
    typcn
    typcn
    Jan 18, 2015❤️2
    2
    如果由于反代等原因 返回了两个 Content-Disposition 即使完全相同也会导致浏览器拒绝下载
    之前就被坑了。。
    Showfom
    Showfom
    PRO
    Jan 18, 2015
    3
    索性可以直接让所有文件都强制下载
    kookxiang
    kookxiang
    Jan 18, 2015❤️6
    4
    <a href="*****.mp4" download>
    <a href="*****.mp4" download="test.mp4">
    这样也行
    ericls
    ericls
    Jan 18, 2015❤️1
    5
    加个download attribute应该可以

    还能设置下载的文件名称
    jiuzhe
    jiuzhe
    Jan 18, 2015
    6
    学习了
    xoxo
    xoxo
    Jan 18, 2015
    8
    Content-Disposition: attachment, filename="xoxo.avi"

    也行,还兼容IE6
    foxwoods
    foxwoods
    Jan 18, 2015❤️1
    9
    加上这个header兼容性会更好,也更安全:

    X-Content-Type-Options: nosniff
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4242 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 07:40 · PVG 15:40 · LAX 23:40 · JFK 02:40
    ♥ Do have faith in what you're doing.