views:

4750

answers:

5

How do I initialize an automatic download of a file in IE?

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 just need to include a meta tag in the header < meta http-equiv="Refresh" content="n;url" > where n is number of seconds and url is the download url. This does not work in IE. Can anyone provide me with a idea of how to make this work in IE browsers?

+6  A: 

I recently solved it by placing the following script on the page.

setTimeout('window.location = 'yoururltofile.file', 5000);

I agree that a meta-refresh would be nicer but if it doesn't work what do you do...

ullmark
+11  A: 

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)

devio
This dont seem to work in IE for me
Dan
Could somebody explain how that should work? I don't see delay for 'n' seconds... Thank you in advance.
Budda
+2  A: 

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.

scunliffe
+3  A: 

I hate when sites complicate download so much and use hacks instead of a good old link.

<a href="file.zip">Start automatic download!</a>

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.
The point is that link to download is real, can be copied, dragged, intercepted by download accelerators, gets :visited color, etc.

porneL
I like the simplicity of your answer.
David Robbins
A: 

This seemed to work for me - across all browsers.

 <script type="text/javascript">
    window.onload = function(){
     document.location = 'somefile.zip';
    }
    </script>
Dan
The lack of such approach is that browser waits for all banners loading... Sometime that takes some time and user is unable to get file due to stupid banners...
Budda