How do I initialize an automatic download of a file in Internet Explorer?
如何在Internet Explorer中初始化一个文件的自动下载?
For example, in the download page, I want the download link to appear and a message: "If you download doesn't start automatically .... etc". The download should begin shortly after the page loads.
例如,在下载页面中,我希望下载链接出现并发送一条消息:“如果下载不自动启动”等”。下载应该在页面加载后不久开始。
In Firefox this is easy, you just need to include a meta tag in the header, <meta http-equiv="Refresh" content="n;url">
where n is the number of seconds and url
is the download URL. This does not work in Internet Explorer. How do I make this work in Internet Explorer browsers?
在Firefox中,这很简单,只要在header中包含一个元标签, ,其中n是秒数,url是下载url。这在Internet Explorer中不起作用。我如何在Internet Explorer浏览器中进行这项工作?
15 个解决方案
#1
96
SourceForge uses an <iframe>
element with the src=""
attribute pointing to the file to download.
SourceForge使用一个
<iframe width="1" height="1" frameborder="0" src="[File location]"></iframe>
(Side effect: no redirect, no JavaScript, original URL remains unchanged.)
(副作用:没有重定向,没有JavaScript,原始URL保持不变。)
#2
46
I hate when sites complicate download so much and use hacks instead of a good old link.
我讨厌网站复杂的下载和使用黑客,而不是一个好的旧的链接。
Dead simple version:
<a href="file.zip">Start automatic download!</a>
It works! In every browser!
它的工作原理!在每个浏览器!
If you want to download a file that is usually displayed inline (such as an image) then HTML5 has a download
attribute that forces download of the file. It also allows you to override filename (although there is a better way to do it):
如果您想下载通常显示为内联的文件(比如图像),那么HTML5有一个下载属性,可以强制下载该文件。它还允许您覆盖文件名(尽管有更好的方法):
<a href="report-generator.php" download="result.xls">Download</a>
Version with a "thanks" page:
If you want to display "thanks" after download, then use:
如果您想在下载后显示“谢谢”,请使用:
<a href="file.zip"
onclick="if (event.button==0)
setTimeout(function(){document.body.innerHTML='thanks!'},500)">
Start automatic download!
</a>
Function in that setTimeout
might be more advanced and e.g. download full page via AJAX (but don't navigate away from the page — don't touch window.location
or activate other links).
setTimeout中的函数可能更高级,例如通过AJAX下载整个页面(但是不要从页面中导航——不要触摸窗口。定位或激活其他链接)。
The point is that link to download is real, can be copied, dragged, intercepted by download accelerators, gets :visited
color, doesn't re-download if page is left open after browser restart, etc.
关键是下载的链接是真实的,可以被下载的加速器复制、拖拽、截取:访问颜色,如果在浏览器重启后页面被打开,就不会重新下载。
That's what I use for ImageOptim
这就是ImageOptim的用途。
#3
19
I had a similar issue and none of the above solutions worked for me. Here's my try (requires jquery):
我有一个类似的问题,上面的解决方案对我都不起作用。下面是我的尝试(需要jquery):
$(function() {
$('a[data-auto-download]').each(function(){
var $this = $(this);
setTimeout(function() {
window.location = $this.attr('href');
}, 2000);
});
});
Usage: Just add an attribute called data-auto-download
to the link pointing to the download in question:
用法:只需将一个名为data-auto-download的属性添加到指向下载的链接中:
<p>The download should start shortly. If it doesn't, click
<a data-auto-download href="/your/file/url">here</a>.</p>
It should work in all cases.
它应该适用于所有情况。
#4
19
I recently solved it by placing the following script on the page.
我最近通过在页面上放置以下脚本解决了这个问题。
setTimeout(function () { window.location = 'my download url'; }, 5000)
I agree that a meta-refresh would be nicer but if it doesn't work what do you do...
我同意,元刷新会更好,但如果它不起作用,你会怎么做…
#5
7
A simple bit of jQuery solved this problem for me.
一个简单的jQuery解决了这个问题。
$(function() {
$(window).bind('load', function() {
$("div.downloadProject").delay(1500).append('<iframe width="0" height="0" frameborder="0" src="[YOUR FILE SRC]"></iframe>');
});
});
In my HTML, I simply have
在我的HTML中,我只是有。
<div class="downloadProject"></div>
All this does is wait a second and a half, then append the div with the iframe referring to the file that you want to download. When the iframe is updated onto the page, your browser downloads the file. Simple as that. :D
所有这些都是等待一秒半的时间,然后将div附加到您想要下载的文件的iframe中。当iframe被更新到页面时,您的浏览器将下载该文件。就这么简单。:D
#6
5
This is what I'm using in some sites (requires jQuery).:
这是我在一些网站上使用的(需要jQuery)。
$(document).ready(function() {
var downloadUrl = "your_file_url";
setTimeout("window.location.assign('" + downloadUrl + "');", 1000);
});
The file is downloaded automatically after 1 second.
文件在1秒后自动下载。
#7
5
I used this, seems working and is just simple JS, no framework:
我用了这个,看起来很好用,只是简单的JS,没有框架:
Your file should start downloading in a few seconds.
If downloading doesn't start automatically
<a id="downloadLink" href="[link to your file]">click here to get your file</a>.
<script>
var downloadTimeout = setTimeout(function () {
window.location = document.getElementById('downloadLink').href;
}, 2000);
</script>
NOTE: this starts the timeout in the moment the page is loaded.
注意:这将在加载页面时启动超时。
#8
4
Works on Chrome, firefox and IE8 and above:
Chrome、firefox、IE8及以上版本:
var link = document.createElement('a');
document.body.appendChild(link);
link.href = url;
link.click();
#9
3
I checked and found, it will work on button click via writing onclick event to Anchor tag or Input button
我检查并找到了,它将在按钮的点击事件上,通过单击事件锚定标签或输入按钮。
onclick='javascript:setTimeout(window.location=[File location], 1000);'
#10
2
Be sure to serve up the file without a no-cache header! IE has issues with this, if user tries to "open" the download without saving first.
确保在没有无缓存标题的情况下提供文件!IE有问题,如果用户尝试“打开”下载而不先保存。
#11
2
Back to the roots, i use this:
回到根,我用这个:
<meta http-equiv="refresh" content="0; url=YOURFILEURL"/>
Maybe not WC3 conform but works perfect on all browsers, no HTML5/JQUERY/Javascript.
也许不符合WC3的标准,但在所有浏览器上都是完美的,没有HTML5/JQUERY/Javascript。
Greetings Tom :)
汤姆:问候)
#12
1
This seemed to work for me - across all browsers.
在所有的浏览器中,这似乎都适用于我。
<script type="text/javascript">
window.onload = function(){
document.location = 'somefile.zip';
}
</script>
#13
1
I think this will work for you. But visitors are easy if they got something in seconds without spending more time and hence they will also again visit your site. <a href="file.zip" onclick="if (event.button==0) setTimeout(function(){document.body.innerHTML='thanks!'},500)"> Start automatic download! </a>
我想这对你有用。但是,如果访客能在几秒钟内得到一些东西,而不用花更多的时间,他们也会再次访问你的网站。< a href = "文件。zip " onclick = "如果(event.button = = 0)setTimeout(函数(){ document.body.innerHTML =“谢谢!”},500)" >开始自动下载!< / >
#14
1
For those trying to trigger the download using a dynamic link it's tricky to get it working consistently across browsers.
对于那些试图使用动态链接来触发下载的人来说,要让它在不同的浏览器之间一致地工作是很困难的。
I had trouble in IE10+ downloading a PDF and used @dandavis' download
function (https://github.com/rndme/download).
我在IE10+下载PDF并使用@dandavis的下载功能(https://github.com/rndme/download)时遇到了麻烦。
IE10+ needs msSaveBlob
.
IE10 msSaveBlob +需求。
#15
0
I hope this will works all the browsers. You can also set the auto download timing.
我希望这能对所有的浏览器都有效。您还可以设置自动下载时间。
<html>
<head>
<title>Start Auto Download file</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$(function() {
$('a[data-auto-download]').each(function(){
var $this = $(this);
setTimeout(function() {
window.location = $this.attr('href');
}, 2000);
});
});
</script>
</head>
<body>
<div class="wrapper">
<p>The download should start shortly. If it doesn't, click
<a data-auto-download href="auto-download.zip">here</a>.</p>
</div>
</body>
</html>
演示
#1
96
SourceForge uses an <iframe>
element with the src=""
attribute pointing to the file to download.
SourceForge使用一个
<iframe width="1" height="1" frameborder="0" src="[File location]"></iframe>
(Side effect: no redirect, no JavaScript, original URL remains unchanged.)
(副作用:没有重定向,没有JavaScript,原始URL保持不变。)
#2
46
I hate when sites complicate download so much and use hacks instead of a good old link.
我讨厌网站复杂的下载和使用黑客,而不是一个好的旧的链接。
Dead simple version:
<a href="file.zip">Start automatic download!</a>
It works! In every browser!
它的工作原理!在每个浏览器!
If you want to download a file that is usually displayed inline (such as an image) then HTML5 has a download
attribute that forces download of the file. It also allows you to override filename (although there is a better way to do it):
如果您想下载通常显示为内联的文件(比如图像),那么HTML5有一个下载属性,可以强制下载该文件。它还允许您覆盖文件名(尽管有更好的方法):
<a href="report-generator.php" download="result.xls">Download</a>
Version with a "thanks" page:
If you want to display "thanks" after download, then use:
如果您想在下载后显示“谢谢”,请使用:
<a href="file.zip"
onclick="if (event.button==0)
setTimeout(function(){document.body.innerHTML='thanks!'},500)">
Start automatic download!
</a>
Function in that setTimeout
might be more advanced and e.g. download full page via AJAX (but don't navigate away from the page — don't touch window.location
or activate other links).
setTimeout中的函数可能更高级,例如通过AJAX下载整个页面(但是不要从页面中导航——不要触摸窗口。定位或激活其他链接)。
The point is that link to download is real, can be copied, dragged, intercepted by download accelerators, gets :visited
color, doesn't re-download if page is left open after browser restart, etc.
关键是下载的链接是真实的,可以被下载的加速器复制、拖拽、截取:访问颜色,如果在浏览器重启后页面被打开,就不会重新下载。
That's what I use for ImageOptim
这就是ImageOptim的用途。
#3
19
I had a similar issue and none of the above solutions worked for me. Here's my try (requires jquery):
我有一个类似的问题,上面的解决方案对我都不起作用。下面是我的尝试(需要jquery):
$(function() {
$('a[data-auto-download]').each(function(){
var $this = $(this);
setTimeout(function() {
window.location = $this.attr('href');
}, 2000);
});
});
Usage: Just add an attribute called data-auto-download
to the link pointing to the download in question:
用法:只需将一个名为data-auto-download的属性添加到指向下载的链接中:
<p>The download should start shortly. If it doesn't, click
<a data-auto-download href="/your/file/url">here</a>.</p>
It should work in all cases.
它应该适用于所有情况。
#4
19
I recently solved it by placing the following script on the page.
我最近通过在页面上放置以下脚本解决了这个问题。
setTimeout(function () { window.location = 'my download url'; }, 5000)
I agree that a meta-refresh would be nicer but if it doesn't work what do you do...
我同意,元刷新会更好,但如果它不起作用,你会怎么做…
#5
7
A simple bit of jQuery solved this problem for me.
一个简单的jQuery解决了这个问题。
$(function() {
$(window).bind('load', function() {
$("div.downloadProject").delay(1500).append('<iframe width="0" height="0" frameborder="0" src="[YOUR FILE SRC]"></iframe>');
});
});
In my HTML, I simply have
在我的HTML中,我只是有。
<div class="downloadProject"></div>
All this does is wait a second and a half, then append the div with the iframe referring to the file that you want to download. When the iframe is updated onto the page, your browser downloads the file. Simple as that. :D
所有这些都是等待一秒半的时间,然后将div附加到您想要下载的文件的iframe中。当iframe被更新到页面时,您的浏览器将下载该文件。就这么简单。:D
#6
5
This is what I'm using in some sites (requires jQuery).:
这是我在一些网站上使用的(需要jQuery)。
$(document).ready(function() {
var downloadUrl = "your_file_url";
setTimeout("window.location.assign('" + downloadUrl + "');", 1000);
});
The file is downloaded automatically after 1 second.
文件在1秒后自动下载。
#7
5
I used this, seems working and is just simple JS, no framework:
我用了这个,看起来很好用,只是简单的JS,没有框架:
Your file should start downloading in a few seconds.
If downloading doesn't start automatically
<a id="downloadLink" href="[link to your file]">click here to get your file</a>.
<script>
var downloadTimeout = setTimeout(function () {
window.location = document.getElementById('downloadLink').href;
}, 2000);
</script>
NOTE: this starts the timeout in the moment the page is loaded.
注意:这将在加载页面时启动超时。
#8
4
Works on Chrome, firefox and IE8 and above:
Chrome、firefox、IE8及以上版本:
var link = document.createElement('a');
document.body.appendChild(link);
link.href = url;
link.click();
#9
3
I checked and found, it will work on button click via writing onclick event to Anchor tag or Input button
我检查并找到了,它将在按钮的点击事件上,通过单击事件锚定标签或输入按钮。
onclick='javascript:setTimeout(window.location=[File location], 1000);'
#10
2
Be sure to serve up the file without a no-cache header! IE has issues with this, if user tries to "open" the download without saving first.
确保在没有无缓存标题的情况下提供文件!IE有问题,如果用户尝试“打开”下载而不先保存。
#11
2
Back to the roots, i use this:
回到根,我用这个:
<meta http-equiv="refresh" content="0; url=YOURFILEURL"/>
Maybe not WC3 conform but works perfect on all browsers, no HTML5/JQUERY/Javascript.
也许不符合WC3的标准,但在所有浏览器上都是完美的,没有HTML5/JQUERY/Javascript。
Greetings Tom :)
汤姆:问候)
#12
1
This seemed to work for me - across all browsers.
在所有的浏览器中,这似乎都适用于我。
<script type="text/javascript">
window.onload = function(){
document.location = 'somefile.zip';
}
</script>
#13
1
I think this will work for you. But visitors are easy if they got something in seconds without spending more time and hence they will also again visit your site. <a href="file.zip" onclick="if (event.button==0) setTimeout(function(){document.body.innerHTML='thanks!'},500)"> Start automatic download! </a>
我想这对你有用。但是,如果访客能在几秒钟内得到一些东西,而不用花更多的时间,他们也会再次访问你的网站。< a href = "文件。zip " onclick = "如果(event.button = = 0)setTimeout(函数(){ document.body.innerHTML =“谢谢!”},500)" >开始自动下载!< / >
#14
1
For those trying to trigger the download using a dynamic link it's tricky to get it working consistently across browsers.
对于那些试图使用动态链接来触发下载的人来说,要让它在不同的浏览器之间一致地工作是很困难的。
I had trouble in IE10+ downloading a PDF and used @dandavis' download
function (https://github.com/rndme/download).
我在IE10+下载PDF并使用@dandavis的下载功能(https://github.com/rndme/download)时遇到了麻烦。
IE10+ needs msSaveBlob
.
IE10 msSaveBlob +需求。
#15
0
I hope this will works all the browsers. You can also set the auto download timing.
我希望这能对所有的浏览器都有效。您还可以设置自动下载时间。
<html>
<head>
<title>Start Auto Download file</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$(function() {
$('a[data-auto-download]').each(function(){
var $this = $(this);
setTimeout(function() {
window.location = $this.attr('href');
}, 2000);
});
});
</script>
</head>
<body>
<div class="wrapper">
<p>The download should start shortly. If it doesn't, click
<a data-auto-download href="auto-download.zip">here</a>.</p>
</div>
</body>
</html>
演示