【问题标题】:HTML5 download attribute not working cross-domain? Can it be made to work? [duplicate]HTML5 下载属性不能跨域工作?它可以工作吗? [复制]
【发布时间】:2015-01-11 07:33:50
【问题描述】:

考虑以下页面:

https://8chan.co/stackoverflow.html

为什么第一个链接有效,而第二个链接无效?我需要发送某个标头,还是无法在子域上使用download 属性?

【问题讨论】:

    标签: html download


    【解决方案1】:

    Chrome 确实允许跨域文件的下载属性,没有 CORS 标头,但 Firefox 选择不这样做,理由是潜在的社会工程攻击。

    检查此链接...HTML5 download attribute not working when downloading from another server, even when Access-Control-Allow-Origin is set to all (*)

    您可以使用 php 代理文件来修复它,例如:

    <?php
    $url = $_GET['file'];
    $name = $_GET['name'];
    header("Content-type: application/$ext");
    header("Content-Disposition: attachment; filename=".$name);
    echo readfile($url);
    ?>
    

    【讨论】:

    • 我知道这是不久前发布的,但是当我使用上述代码时,mp4 文件已损坏...?
    • 最好不要添加尾随?&gt;,因为它可能会破坏输出
    • 什么是$ext?那应该是文件扩展名,但变量没有定义
    • “Chrome 实际上确实允许跨域文件的下载属性”——我想目前 Chrome 也禁用了跨域下载属性
    • 此代码允许标头注入并为 Internet 上的任何文件提供代理。 这应该被认为是不安全的。不要在没有白名单的情况下构建这样的东西!
    猜你喜欢
    • 1970-01-01
    • 2013-01-11
    • 2013-02-24
    • 2013-12-22
    • 1970-01-01
    • 2020-12-12
    • 2014-03-17
    • 2011-09-27
    • 1970-01-01
    相关资源
    最近更新 更多