如何使Safari和IE下载图像而不是在新页面中打开?

时间:2013-09-02 17:46:47

标签: html internet-explorer web hyperlink safari

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

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

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

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

4 个答案:

答案 0 :(得分:14)

你在使用Apache服务器吗?如果是这样,您可以将其添加到.htaccess文件中:

/// <summary>
///     Calculates the date with a number of working days offset from today by
///     calculating a set of dates that exclude weekends and holidays, then
///     skipping forward the required number of days.
/// </summary>
public static DateTime AddWorkingDays(int workingDays, DateTime startDate)
{
    var workDaysQuery = from n in Enumerable.Range(0, (workingDays + 14) * 2)
                        let date = startDate.AddDays(n)
                        where (date.DayOfWeek != DayOfWeek.Sunday) && (date.DayOfWeek != DayOfWeek.Saturday)
                        select date;
    var publicHolidays = GetPublicHolidays();

    var daysWorking = workDaysQuery.Except(publicHolidays).ToArray(); // why does this still include the publicHolidays ?

    return daysWorking.Skip(workingDays).First();
}

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

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

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

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

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

编辑(2016年10月4日):

所有浏览器看起来<?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> 属性仍为not fully adopted

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

答案 1 :(得分:0)

这是一种使用JavaScript在IE中执行此操作的方法。但是,我找不到Safari的解决方案

var canvas = document.createElement('canvas');
var img = document.createElement('img');
img.onload = function(e) {
    canvas.width = img.width;
    canvas.height = img.height;
    var context = canvas.getContext('2d');
    context.drawImage( img, 0, 0, img.width, img.height);
    window.navigator.msSaveBlob(canvas.msToBlob(),'image.jpg');
}
img.src = 'img.jpg;

答案 2 :(得分:-1)

最简单的方法是使用像PHP这样的服务器端语言。本页面对此进行了更深入的讨论,包括一些用于操作标题的试用代码等。不幸的是,在IE和Safari赶上之前,这些是唯一的解决方案。

Refrence:

答案 3 :(得分:-1)

navigator.msSaveBlob(blob, filename)

https://msdn.microsoft.com/sv-se/library/windows/apps/hh772331

不幸的是,我不知道如何在Safari中做到这一点。