gpt4 book ai didi

html - 如何让Safari和IE下载图片而不是在新页面打开?

转载 作者:技术小花猫 更新时间:2023-10-29 12:02:10 24 4
gpt4 key购买 nike

在 Firefox 和 Chrome 中,此链接属性“download=img.jpg”工作正常并显示下载窗口而不是新选项卡或页面。

<a href="img.jpg" download="img.jpg">Download Image</a>

但在 Safari 和 IE 中,此链接为我提供了一个新页面。

那么用 Safari 和 IE 浏览器处理这个问题的简单有效的工作流程是什么?

最佳答案

您在 Apache 服务器上工作吗?如果是这样,您可以将其添加到您的 .htaccess 文件中:

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{QUERY_STRING} fdl=1
RewriteRule .? - [T=application/octet-stream]

检查它是一个文件检查参数 fdl=1 是否在查询字符串中输出为八位字节流/强制下载

现在,当您希望浏览器开始下载该站点中的任何内容时,只需将参数放在 url 上:

<a href="img.jpg?fdl=1">Download Image</a>

要在 IIS Windows 服务器上执行相同的操作,请将出站规则添加到 web.config 中:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<outboundRules>
<rule name="Force Download">
<match serverVariable="RESPONSE_Content_Disposition" pattern=".?" negate="false" />
<action type="Rewrite" value="application/octet-stream" replace="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="fdl=1" />
</conditions>
</rule>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>

编辑(10/4/2016):

看起来 download 属性仍然是 not fully adopted所有浏览器。

对于基于 JavaScript/浏览器的实现,您可以查看 FileSaver.js这是一个 polyfill,用于在不支持它的浏览器中保存功能。虽然它没有完美的覆盖范围。

关于html - 如何让Safari和IE下载图片而不是在新页面打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18578473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com