views:

36

answers:

2

I have seen many websites triggering a file save-as dialog on a page with existing HTML content. How do they do this?

I know about setting the right headers such as Content-disposition etc. but when I do that, the content of the page does not load, and immediately the file download is triggered...

A: 

I know a lot of them use a hidden iframe, the page that the iframe points towards is actually a file download.

Although there are better ways to do this, I'm sure.

Joshua
Yeah iframe is one method I seemed to find... But it's not that widely supported, like in Opera... I was wondering if there are any other techniques.
jeffreyveon
Slaks answer would work, and is probably the best way to do it. :)
Joshua
+1  A: 

Make a regular HTML page, then add a META refresh that redirects to a PHP script that sends the file with a Content-Disposition header.

For example:

<meta http-equiv="refresh" content="2;url=download.php">
SLaks
So, just to clarify, this would refresh the page and therefore download the file at download.php, but leave the page being viewed intact? If so, great answer!
Kurucu
Yes. ​​It would.
SLaks