View Full Version : Save As Dialog without new Window (AJAX)

11-03-2006, 10:20 PM
Hey guys, I've been experimenting around with a way to get a "Save As" dialog to pop up prompting the user to download a certain file from the server.
How I've done this so far is open a window in Javascript to a PHP file, server side, passing an argument through _GET. The PHP generates the file and then sends a header request to download. Simple enough. The problem is that using this method opens a blank window in addition to the Save As Dialog. Is there anyway to fool the browser/ hide the window/ etc, so two windows don't pop up at the same time everytime my users press the download button?
I am also pretty familiar with AJAX techniques, so if anyone has a solution along that route as well I'd appreciate it.


11-04-2006, 12:09 AM
There maybe a better way but you could target a hidden frame or IFrame. so your link could be:

<a href="script.php" target="hiddenIframe">Click here</a>
<iframe src="javascript:false" name="hiddenIframe" style="display:none"></iframe>

this will stop the second window appearing.

11-04-2006, 02:04 AM
I don't use PHP, so I'm not sure how you would do this with PHP.

I have a PERL/CGI script that I use (download.pl) to allow users to download files (and to keep them from downloading more than two files at a time)

In PERL, I just change the content-disposition in the HTTP header to be:

Content-disposition: attachment

When the user clicks a link that returns a content-dispostition of attachment the page doesn't change and a download dialog opens.

12-15-2006, 09:10 PM
Nice got it to work with the hidden iframe. great solution.


12-15-2006, 09:53 PM
You can change the header in PHP just as easily as in PERL.

header('Content-Disposition: attachment');

A simple HTML link to the PHP will then offer it for download.

07-04-2011, 04:26 AM
Hello quakerstate79

I working on similar requirement. I have a zip file in the server at one particular location. can you help me with some code snippet

like how to trigger a PHP to access the zip file( server) and then sends a header request to download the zip file.

I am not Java Script or PHP guru. any help is greatly appreciated.