从另一台服务器下载时 HTML5 下载属性不起作用,即使 Access-Control-Allow-Origin 设置为全部 (*)

标签 html http download attributes cross-domain

我有一个像这样的下载链接:

<a href="foo.xls" download="bar.xls">Foobar</a>

这在同一台服务器上下载文件时工作正常,但从另一台服务器(在本例中为 Azure blob 存储)下载时,文件名保持为“foo.xls”,即使 HTTP 响应返回以下 header :

Access-Control-Allow-Origin: *



这是设计使然还是我可以将另一个 header 添加到 HTTP 响应中以使其正常工作?

最佳答案

是的,CORS header 对 download 没有影响是设计使然。属性。只有两种浏览器支持 download属性,Firefox 和 Chrome,并且两种浏览器对跨源文件都有不同的策略。

Chrome 版本 prior to 65实际上确实允许 download跨源文件的属性,没有 CORS header ,但 Firefox 选择不这样做,理由是潜在的社会工程攻击。

MDN 在 download attribute section for the a tag 下记录了 Firefox 20 的这种行为。 ,从那以后没有改变的行为。

In Firefox 20 this attribute is only honored for links to resources with the same-origin.



This Bugzilla report讨论了安全问题和使用 CORS 的可能性。

When the user clicks such a link, the user will be prompted if they want to download. It seems very easy for the user to make the mistake of thinking that something on the original website is being downloaded, and not something from bank.com.



Would it be possible to implement it with same-origin and CORS (Access-Control-Allow-Origin) in mind if you are questioning cross origin security? This is very useful feature for web applications (create Blob using JS and let user download it with some meaningful name)



Google 反对为此使用 CORS。


还有this Bugzilla report ,从另一个错误报告总结了他们的决定。

Also, cross origin downloads are working perfectly in Google Chrome.



是的,我们认为他们这样做会增加安全漏洞。

Bugzilla 问题似乎不排除将 CORS 用于跨域的可能性 download将来支持属性,但现在使用 CORS header 不会对 download 做任何事情属性。如果其他浏览器开始支持该属性,则可能尚未达成共识。

为了完整起见,当然还有 Content-Disposition您可以使用该 header 强制从其他域下载,但这不提供与 download 相同的功能属性。不过它确实有更好的浏览器支持。

关于从另一台服务器下载时 HTML5 下载属性不起作用,即使 Access-Control-Allow-Origin 设置为全部 (*),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25286108/

相关文章:

在 Windows 资源管理器中打开时,PHP .zip 文件下载错误

java - JAXP 可以用来创建 HTML5 文档吗?

javascript - 如何在 anchor 悬停时在左下角显示与所述 anchor 的 href 属性不同的链接?

http - 浏览器完整性检查 - NGINX

java - 将 POST 数据从一个 servlet 调用到另一台服务器

linux - 在Linux中定期从FTP下载文件

html - 如何仅通过正 tabindex 导航

html - 我有 3 列,滚动时需要让中间列保持不变

http - golang 服务器 : how to retrieve multiple files continuously

java - 通过java通过https下载文件