13

I have a web form that users complete online. When they press submit it will start a file download for them.

At the moment, I process the form submission and generate a suitable file for the user and fire it off with suitable headers. eg...

header('Content-type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="yourfile.txt"');
header("Content-Transfer-Encoding: binary");

However, since this starts a download right away, the original form is left on screen by the browser.

I would really like to go to some "Thank you" screen once the download completes (or before the download starts). I know it is possible, because almost every download site you visit does this (normally to pump you full of adverts before the download starts).

So, How do I show a "Thank You" screen that starts the download after a second?

How would any solution proposed effect the behaviour of the back button, as I don't want the file downloading again without the form being refilled?

I am using PHP on the server and can rely on Javascript (and jQuery) being available on the client.

Thank you.

Rik Heywood
  • 13,816
  • 9
  • 61
  • 81

3 Answers3

24

You could send the form to the Thank you document and put there a META refresh to the file download:

<meta http-equiv="refresh" content="3;url=download.php">
<p>Thank you! The download will start in 3 seconds. If not, use this link to download the <a href="download.php">file</a></p>
Gumbo
  • 643,351
  • 109
  • 780
  • 844
3

Add a second page that says something "thank you, your donwnload will start in a few seconds" and triggers the download using javascript:

$(document).ready(function(){
  window.setTimeout(function(){
    window.location = 'http://yourdownloadhost.com/file.zip';
  }, 1500);
});

or use a meta redirect.

middus
  • 9,103
  • 1
  • 31
  • 33
  • Well, as you can see from my comment to Gumbo's post, I myself prefer META redirect. However, I was kind of mislead by the OP's "can rely on Javascript (and jQuery)". However, some templating system's make it hard to inject stuff into the . So sometimes JS might be an option. – middus Oct 02 '09 at 12:57
  • Ought to be in the header too. AFAIK javascript in the body are illegal. – Georg Schölly Oct 02 '09 at 14:12
  • 3
    No, JS in the body is perfectly fine. See, e.g., Google Analytics. – middus Oct 04 '09 at 11:27
0

You can insert a hidden iframe into your page and submit your form to this iframe.