Page 1 of 1
I need to close the browser window after a force-download
Posted: Thu Dec 14, 2006 3:27 pm
by intergroove
I am scripting a download service, as the website is on one server and the download files are on another I am using a pop-up window to launch a php script. The script produces a force-download. All of this works fine - but I'd like to close the window.
Here is my force download:
Code: Select all
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($file)."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($file));
set_time_limit(0);
@readfile($file) or die("File not found.");
How would I go about adding a javascipt element to the code to enforce the close?
Any help greatly appreciated.
Re: I need to close the browser window after a force-downloa
Posted: Thu Dec 14, 2006 4:06 pm
by calevans
intergroove wrote:I am scripting a download service, as the website is on one server and the download files are on another I am using a pop-up window to launch a php script. The script produces a force-download. All of this works fine - but I'd like to close the window.
Here is my force download:
Code: Select all
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename="".basename($file)."";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($file));
set_time_limit(0);
@readfile($file) or die("File not found.");
How would I go about adding a JavaScript element to the code to enforce the close?
Any help greatly appreciated.
If I'm not mistaken, you simply add:
<html>
<body>
<script>
document.close(); // possibly window.close();
</script>
</body>
</html>
I could be wrong though. This of course will close the window whether the file downloads ot not since it does not honor the die() in PHP. So even if this works, it may not be the optimal solution.
=C=
Posted: Thu Dec 14, 2006 4:11 pm
by intergroove
I tried that method, but HTML wasn't executed as the headers where pushed out first (this what I think is happening - bit ofa novice in this area).
Posted: Thu Dec 14, 2006 4:14 pm
by volka
intergroove wrote:as the website is on one server and the download files are on another I am using a pop-up window to launch a php script. The script produces a force-download.
I don't understand. Why exactly is it you have a pop-up?
SOLUTION
Posted: Thu Dec 14, 2006 4:17 pm
by intergroove
I have come up with a solution:
Send the launch script to an invisible IFRAME. Then I don't have to think about closing the window. Eureka!
So I use the following HTML:
Code: Select all
<iframe src="blank.htm" name="dloader"
id="dloader" width="0" height="0" border="0"
frameborder="0" scrolling=no marginwidth="0"
marginheight="0" vspace="0"
hspace="0"></iframe>
where blank.htm is an empty html file (just: <html><header></header><body></body></html> )
Now my download link launches the php script into 'dloader'
If anyone knows of any just impedement (ie will this work on all browsers) then let me know asap.
chrs
Posted: Thu Dec 14, 2006 4:20 pm
by intergroove
volka wrote:intergroove wrote:as the website is on one server and the download files are on another I am using a pop-up window to launch a php script. The script produces a force-download.
I don't understand. Why exactly is it you have a pop-up?
Non-lateral thinking. Designed this early in the project - have just been piecing everything together when I realised the pop-up hangs around (I was just happy toget the force-download to work when I first scripted it). The IFrame solution seems good at the moment though.
Any other suggestions/opinions gratefully received.
Posted: Thu Dec 14, 2006 4:44 pm
by volka
intergroove wrote:Send the launch script to an invisible IFRAME. Then I don't have to think about closing the window. Eureka!
Forgive me for beeing dumb but why do you need "another" target window/frame at all? This is no rethorical question; I really do not understand what you're trying to achieve with it. I take it you have some sort of link to download something, simple example
but for some reason you seem to have
Code: Select all
<a href="download.php?id=4711" target="xyz">
But why?
Posted: Thu Dec 14, 2006 7:42 pm
by intergroove
volka wrote:intergroove wrote:Send the launch script to an invisible IFRAME. Then I don't have to think about closing the window. Eureka!
Forgive me for beeing dumb but why do you need "another" target window/frame at all? This is no rethorical question; I really do not understand what you're trying to achieve with it. I take it you have some sort of link to download something, simple example
but for some reason you seem to have
Code: Select all
<a href="download.php?id=4711" target="xyz">
But why?
My download files are stored on a different server below public level. I need to keep the original page in place while force-downloading a file.
The force-download script is on the file server. It needs to be there to get to the files below the root.
If I link directly to it my page refreshes to the blank download page.
Does that make sense? I guess being a lone developer I'm going to make incorrect assumptions in places and use crazy unessessary work arounds. But I try my best.
Posted: Thu Dec 14, 2006 10:16 pm
by Kieran Huggins
This is not a php issue - you have to do this in the client.
I would use a javascript function to trigger the page load in a new window (the forced download) then self.close() the current window.
Cheers,
Kieran
Posted: Fri Dec 15, 2006 2:37 am
by volka
intergroove wrote:If I link directly to it my page refreshes to the blank download page.
Take a look at
http://httpd.apache.org/download.cgi
There's a section "Apache HTTP Server x.y.z is the best available version" and a link e.g. to the unix sources. The link points to a mirror, not an apache.org server. Do you get a new,blank windows when clicking the link?
Posted: Fri Dec 15, 2006 3:09 am
by intergroove
Kieran Huggins wrote:This is not a php issue - you have to do this in the client.
I would use a javascript function to trigger the page load in a new window (the forced download) then self.close() the current window.
Cheers,
Kieran
I have tried the javascript close methods but these have failed.
Posted: Fri Dec 15, 2006 3:14 am
by intergroove
volka wrote:intergroove wrote:If I link directly to it my page refreshes to the blank download page.
Take a look at
http://httpd.apache.org/download.cgi
There's a section "Apache HTTP Server x.y.z is the best available version" and a link e.g. to the unix sources. The link points to a mirror, not an apache.org server. Do you get a new,blank windows when clicking the link?
Much as I appreciate your help - at this juncture I am already past the deadline on my development, so brevity is the key.
If you have a prefered method then please explain it rather than send me on a teachers' style 'journey of discovery'.
I have spent far too long looking for a solution - I've found one that works - it would be nice to know the 'BEST' method but I really haven't got time for a goose chase.
Don't take this the wrong way - as you realise it gets pretty stressful when you go past your scheduled deadlines (+3 wks now).
Posted: Fri Dec 15, 2006 3:49 am
by volka
intergroove wrote:at this juncture I am already past the deadline on my development, so brevity is the key.
Then spare me the rest of your previous post. Click the link and answer "yes, it opens a new window" or "no, it does not".
Posted: Fri Dec 15, 2006 3:49 am
by intergroove
Having said that Volka you are right. It launched a new window on the first few attempts (early on) so as I pointed out before - I made some assumptions.
If you had been explicit in your reply I might have cottoned on sooner...
But cheers anyway.
Posted: Fri Dec 15, 2006 3:02 pm
by RobertGonzalez
Ok, enough bickering.
@intergroove - In your popup window, since you are forcing the download, why not close the window after the download is forced to begin. Something like as the window is built (the HTML is pushed to the client) just before the closing BODY tag, put a call to a javaScript Function that you make called closeWindow(). Then in this function put the self.close() or window.close() call.
PS | This is a client side problem and is being moved to client side.