<p>On a PHP-based web site, I want to send users a download package after they have filled out a short form. The site-initiated download should be similar to sites like download.com, which say "your download will begin in a moment."</p>

<p>A couple of <strong>possible approaches</strong> I know about, and browser compatibility (based on a quick test):</p>

<p><strong>1) Do a <code>window.open</code> pointing to the new file.</strong>  </p>

<pre><code>- FireFox 3 blocks this.  
 - IE6 blocks this.  
 - IE7 blocks this.
</code></pre>

<p><strong>2) Create an iframe pointing to the new file.</strong>  </p>

<pre><code>- FireFox 3 seems to think this is OK. (Maybe it's because I already accepted it once?)  
 - IE6 blocks this.  
 - IE7 blocks this.

How can I do this so that at least these three browsers will not object? 
</code></pre>

<p>Bonus: is there a method that doesn't require browser-conditional statements? </p>

<p>(I believe that download.com employs both methods conditionally, but I can't get either one to work.)</p>

<p><strong>Responses and Clarifications:</strong></p>

<pre><code>Q: "Why not point the current window to the file?"  
A: That might work, but in this particular case, I want to show them some other content while their download starts - for example, "would you like to donate to this project?"
</code></pre>

<p><strong>UPDATE: I have abandoned this approach. See my answer below for reasons.</strong></p>

[{'answer_id': 60598, 'author': 'aib', 'author_id': 1088, 'author_profile': 'https://Stackoverflow.com/users/1088', 'pm_score': 0, 'selected': False, 'text': '<p>How about changing the location to point to the new file? (e.g. by changing window.location)</p>\n'}, {'answer_id': 60603, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': 3, 'selected': False, 'text': '<p>I usually just have a PHP script that outputs the file directly to the browser with the appropriate Content-Type</p>\n\n<pre><code>if(file_exists($filename)) {\n    header("Pragma:  public");\n    header("Expires:  0");\n    header("Cache-Control:  must-revalidate, pre-check=0");\n    header("Cache-Control:  private", false);\n    header("Content-Type:  " . $content-type);\n    header("Content-Disposition:  attachment; filename=\\"" . basename($filename) . "\\";" );\n    header("Content-Transfer-Encoding:  binary");\n    header("Content-Length:  " . filesize($filename));\n\n    readfile("$filename");\n}else{\n    print "ERROR:  the file " . basename($filename) . " could not be downloaded because it did not exist.";\n}\n</code></pre>\n\n<p>The only disadvantage is that, since this sets the HTTP header, it has be called before you have any other output.</p>\n\n<p>But you can have a link to the PHP download page and it will cause the browser to pop up a download box without messing up the content of the current page.</p>\n'}, {'answer_id': 60606, 'author': 'Soldarnal', 'author_id': 3420, 'author_profile': 'https://Stackoverflow.com/users/3420', 'pm_score': 5, 'selected': True, 'text': '<p>You can also do a meta refresh, which most browsers support.  Download.com places one in a noscript tag.</p>\n\n<pre><code>&lt;meta http-equiv="refresh" content="5;url=/download.php?doc=123.zip"/&gt;\n</code></pre>\n'}, {'answer_id': 60619, 'author': 'Dan Walker', 'author_id': 752, 'author_profile': 'https://Stackoverflow.com/users/752', 'pm_score': 0, 'selected': False, 'text': '<p>I\'ve always just made an iframe which points to the file.</p>\n\n<pre><code>&lt;iframe src="/download.exe" frameborder="0" height="0" width="0"&gt;&lt;a href="/download.exe"&gt;Click here to download.&lt;/a&gt;&lt;/iframe&gt;\n</code></pre>\n'}, {'answer_id': 62248, 'author': 'Effata', 'author_id': 5740, 'author_profile': 'https://Stackoverflow.com/users/5740', 'pm_score': 0, 'selected': False, 'text': '<p>Regarding not pointing the current window to the download.\nIn my experience you can still show your "please donate" page, since downloads (as long as they send the correct headers) don\'t actually update the browser window.\nI do this for csv exports on one of my sites, and as far as the user is concerned it just pops up a safe file window.\nSo i would recommend a simple meta-redirect as Soldarnal showed.</p>\n'}, {'answer_id': 62495, 'author': 'Vitaly Sharovatov', 'author_id': 6647, 'author_profile': 'https://Stackoverflow.com/users/6647', 'pm_score': 0, 'selected': False, 'text': '<p>Just to summarise, you have 2 goals: </p>\n\n<ol>\n<li>start download process</li>\n<li>show user a page with a donate options</li>\n</ol>\n\n<p>To achieve this I would do the following:</p>\n\n<p>When your user submits the form, he gets the resulting page with a donate options and a text saying that his download will start in 5 seconds. And in the head section of this page you put the META code as Soldarnal said:\n&lt;meta http-equiv="refresh" content="5;url=/download.php?doc=123.zip&gt;</p>\n\n<p>And that\'s all.</p>\n'}, {'answer_id': 62899, 'author': 'scunliffe', 'author_id': 6144, 'author_profile': 'https://Stackoverflow.com/users/6144', 'pm_score': 1, 'selected': False, 'text': '<p>One catch is that you may encounter issues with IE (version 6 in particular) if the headers are not set up "correctly".</p>\n\n<p>Ensure you set the right Content-Type, but also consider setting the Cache options for IE (at least) to <em>allow</em> caching.  If the file is one the user can open rather than save (e.g. an MS Word document) early versions of IE need to cache the file, as they hand off the "open" request to the applicable app, pointing to the file that was downloaded in the cache.</p>\n\n<p>There\'s also a related issue, if the IE6 user\'s cache is full, it won\'t properly save the file (thus when the applicable app gets the hand off to open it, it will complain the file is corrupt.</p>\n\n<p>You may also want to turn of any gzip actions on the downloads too (for IE)</p>\n\n<p>IE6/IE7 both have issues with large downloads (e.g. 4.x Gigs...) not a likely scenario since IE doesn\'t even have a download manager, but something to be aware of.</p>\n\n<p>Finally, IE6 sometimes doesn\'t nicely handle a download "push" if it was initiated from within a nested iframe.  I\'m not exactly sure what triggers the issue, but I find it is easier with IE6 to avoid this scenario.</p>\n'}, {'answer_id': 73190, 'author': 'Nathan Long', 'author_id': 4376, 'author_profile': 'https://Stackoverflow.com/users/4376', 'pm_score': 4, 'selected': False, 'text': '<p>Update: I have decided to abandon this approach, and instead just present the user with a link to the actual file. My reasoning is this:</p>\n\n<p>My initial attempts at a server-initiated download were blocked by the browser. That got me thinking: "the browser is right. How does <strong>it</strong> know that this is a legitimate download? It <strong>should</strong> block a download that isn\'t obviously user-initiated."</p>\n\n<p>Any method that I can use for a server-initiated download could <strong>also</strong> be used by someone who wants to send malware. Therefore, downloads should only happen when the user specifically requests the file by clicking on a link for it.</p>\n\n<p>You\'re free to disagree, and if you still want to initiate a download, hopefully this thread will help you do it.</p>\n'}, {'answer_id': 114732, 'author': 'bastiandoeen', 'author_id': 371953, 'author_profile': 'https://Stackoverflow.com/users/371953', 'pm_score': 1, 'selected': False, 'text': '<p>Hoi!</p>\n\n<p>@Nathan:\nI decided to do exactly that: Have my "getfile.php" load all necessary stuff and then do a  </p>\n\n<pre><code>header("Location: ./$path/$filename");\n</code></pre>\n\n<p>to let the browser itself and directly do whatever it thinks is correct do with the file. This even works fine in Opera with me. </p>\n\n<p>But this will be a problem in environments, where no direct access to the files is allowed, in that case you will have to find a different way! (Thank Discordia my files are public PDFs!)</p>\n\n<p>Best regards, Basty</p>\n'}, {'answer_id': 728009, 'author': 'Kornel', 'author_id': 27009, 'author_profile': 'https://Stackoverflow.com/users/27009', 'pm_score': 0, 'selected': False, 'text': '<pre><code>&lt;a href="normaldownload.zip" onclick="use_dhtml_or_ajax_to_display_page()"&gt;\n</code></pre>\n\n<p>Current page is unaffected if download is saved. Just ensure that download doesn\'t open in the same window (proper MIME type or <code>Content-Disposition</code>) and you\'ll be able to show anything.</p>\n\n<p><a href="https://stackoverflow.com/a/300517">See more complete answer</a></p>\n'}, {'answer_id': 29692393, 'author': 'sagunms', 'author_id': 1297184, 'author_profile': 'https://Stackoverflow.com/users/1297184', 'pm_score': 0, 'selected': False, 'text': '<p>You can use Javascript/jQuery to initiate the download. Here\'s an example - you can get rid of the Ajax request and just use the setTimeout() block.</p>\n\n<pre><code>$("btnDownloadCSV").on(\'click\', function() {\n    $.ajax({\n        url: "php_backend/get_download_url",\n        type: \'post\',\n        contentType: "application/x-www-form-urlencoded",\n        data: {somedata: "somedata"},\n        success: function(data) {\n            // If iFrame already exists, remove it.\n            if($("[id^=\'iframeTempCSV_"]).length) { \n                $("[id^=\'iframeTempCSV_"]).remove();\n            }\n            setTimeout(function() {\n                //  If I\'m creating an iframe with the same id, it will permit download only the first time.\n                // So randHashId appended to ID to trick the browser.\n                var randHashId = Math.random().toString(36).substr(2);\n                // Create a fresh iFrame for auto-downloading CSV\n                $(\'&lt;iframe id="iframeTempCSV_\'+randHashId+\'" style="display:none;" src="\'+data.filepath+\'"&gt;&lt;/iframe&gt;\').appendTo(\'body\');\n            }, 1000);\n        },\n        error: function(xhr, textStatus, errorThrown) {\n           console.error("Error downloading...");\n       }\n    });\n});\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/60590', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4376/']