如何开始自动下载 Internet Explorer 档案?

如何以 Internet Explorer 初始化自动下载档案?

例如,在下载页面中,我希望出现下载链接和一条消息: “如果您的下载不会自动启动..。等等”。下载应该在页面加载后不久开始。

在 Firefox 中,这很简单,只需要在头部包含一个 meta 标记,其中 n 表示秒数,url表示下载 URL。这在 Internet Explorer 里行不通。如何在 Internet Explorer 浏览器中实现这一点?

296362 次浏览

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...

SourceForge uses an <iframe> element with the src="" attribute pointing to the file to download.

<iframe width="1" height="1" frameborder="0" src="[File location]"></iframe>

(Side effect: no redirect, no JavaScript, original URL remains unchanged.)

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.

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):

<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).

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

This seemed to work for me - across all browsers.

 <script type="text/javascript">
window.onload = function(){
document.location = 'somefile.zip';
}
</script>

A simple bit of jQuery solved this problem for me.

$(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

<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

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>

I had a similar issue and none of the above solutions worked for me. Here's my try (requires 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:

<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.

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);'

I used this, seems working and is just simple JS, no framework:

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.

This is what I'm using in some sites (requires jQuery).:

$(document).ready(function() {
var downloadUrl = "your_file_url";
setTimeout("window.location.assign('" + downloadUrl + "');", 1000);
});

The file is downloaded automatically after 1 second.

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+ needs msSaveBlob.

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.

Greetings Tom :)

Works on Chrome, firefox and IE8 and above:

var link = document.createElement('a');
document.body.appendChild(link);
link.href = url;
link.click();

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>

Nice jquery solution:

jQuery('a.auto-start').get(0).click();

You can even set different file name for download inside <a> tag:

Your download should start shortly. If not - you can use
<a href="/attachments-31-3d4c8970.zip" download="attachments-31.zip" class="download auto-start">direct link</a>.

One more :

var a = document.createElement('a');
a.setAttribute('href', dataUri);
a.setAttribute('download', filename);


var aj = $(a);
aj.appendTo('body');
aj[0].click();
aj.remove();
<meta http-equiv="Refresh" content="n;url">

That's It. Easy, Right?

<meta http-equiv="Refresh" content="n;url">

This is an old question but in case anyone wants to use automatic download of files with Flask, Python. You can do this:

from flask import Flask, make_response, send_from_directory


file_path = "Path containing the file" #e.g Uploads/images


@app.route("/download/<file_name>")
def download_file(file_name):
resp = make_response(send_from_directory(file_path, file_name)
resp.headers['Content-Disposition'] = f"attachment; filename={file_name}"
return resp

Inside a template or html page, index for example

<div>
<a class="btn btn-outline-warning" href=\{\{url_for( 'download_file', name='image.png' )}} ">Download Image</a>
</div>

Clicking on the link will download the file without opening another page. For more info on: