How to start automatic download of a file in Inter

2019-01-03 05:08发布

How do I initialize an automatic download of a file in 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?

15条回答
叼着烟拽天下
2楼-- · 2019-01-03 05:22

Works on Chrome, firefox and IE8 and above:

var link = document.createElement('a');
document.body.appendChild(link);
link.href = url;
link.click();
查看更多
太酷不给撩
3楼-- · 2019-01-03 05:23

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.

查看更多
Animai°情兽
4楼-- · 2019-01-03 05:25

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楼-- · 2019-01-03 05:25

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.

查看更多
我命由我不由天
6楼-- · 2019-01-03 05:26

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.

查看更多
▲ chillily
7楼-- · 2019-01-03 05:38

This seemed to work for me - across all browsers.

 <script type="text/javascript">
    window.onload = function(){
     document.location = 'somefile.zip';
    }
    </script>
查看更多
登录 后发表回答