tags:

views:

362

answers:

4

What is the best cross browser way to open a download dialog (lets assume we can set content-disposion:attachment in the headers) without navigating away from the current page, or opening popups, which doesnt work well in IE6.

+2  A: 

Put this in the HTML head section, setting the url var to the URL of the file to be downloaded:

<script type="text/javascipt">  
function startDownload()  
{  
     var url='http://server/folder/file.ext';    
     window.open(url,'Download');  
}  
</script>

Then put this in the body, which will start the download automatically after 5 seconds:

<script type="text/javascipt">  
setTimeout("startDownload()",5000); //starts download after 5 seconds  
</script>

(From here.)

gclaghorn
that doesnt work, because in IE6, if the user clicks "save" the file is saved, but the popup stays open. This is not acceptable.
mkoryak
A: 

How about:

<meta http-equiv="refresh" content="5;url=http://site.com/file.ext"&gt;

This way works on all browsers (i think) and let you put a message like: "If the download doesn't start in five seconds, click here."

If you need it to be with javascript.. well...

document.write('<meta http-equiv="refresh" content="5;url=http://site.com/file.ext"&gt;');

Regards

+2  A: 

I always add a target="_blank" to the download link. This will open a new window, but as soon as the user clicks save, the new window is closed.

jao
A: 

A small/hidden iframe can work for this purpose.

That way you don't have to worry about closing the pop up.

Simon