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

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

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

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

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

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