如何让Safari和IE下载图片而不是在新页面中打开? [英] How to make Safari and IE download image instead of opening in a new page?

查看:290
本文介绍了如何让Safari和IE下载图片而不是在新页面中打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < a href =img.jpgdownload =img.jpg>下载图片< / a> 

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



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

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

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

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



现在,如果您希望浏览器在该站点中开始下载任何内容,将参数放在网址中:

 < a href =img.jpg?fdl = 1>下载图片< / A> 

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

 <?xml version =1.0encoding =UTF-8?> 
<配置>
< system.webServer>
< rewrite>
< outboundRules>
<规则名称=强制下载>
< action type =Rewritevalue =application / octet-streamreplace =true/>
<条件>
< add input ={QUERY_STRING}pattern =fdl = 1/>
< / conditions>
< / rule>
< / outboundRules>
< / rewrite>
< /system.webServer>
< / configuration>

编辑(10/4/2016):



看起来像下载属性仍然是未完全采用所有浏览器。



对于基于JavaScript /浏览器的实现,您可以查看 FileSaver.js ,这是一个在不支持它的浏览器中保存功能的polyfill。尽管它没有完美的报道。


In Firefox and Chrome this link property "download=img.jpg" works fine and shows the download window instead of a new tab or page.

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

But in Safari and IE this link gives me a new page.

So what is a simple and effective workflow to handle this with Safari and IE browsers?

解决方案

Are you working on an Apache server? If so, you can just add this to your .htaccess file:

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

Checks to see it's a file Checks if parameter fdl=1 is in querystring Output as octet-stream/force download

Now when you want the browser to start downloading anything in that site, just put the parameter on the url:

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

To do the same thing on a IIS windows server add the outbound rule to the 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>

EDIT (10/4/2016):

Looks like the download attribute is still not fully adopted by all the browsers.

For a JavaScript / browser based implementation you could look at FileSaver.js which is a polyfill for saving functionality in browsers that don't support it. It doesn't have perfect coverage though.

这篇关于如何让Safari和IE下载图片而不是在新页面中打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆