Lingering Window

C

Chuck Tomasi

I'm clearly missing something and I've been looking for a solution for hours
without a clue.

Objective: Create a typical "download" link using JS window.open. I get the
window to open, the "Save As" box appears, and the file ends up where I want
it, with the right name, the problem is that the child window doesn't go
away (save IE). Mozilla Firefox (PC and Mac), Safari, and all others I've
tested leave the child window. If I do a win.close() the child window
appears and disappears so fast that the SaveAs never shows up. I've tried
using a setTimeout() call to delay this a bit until everything is ready, but
that doesn't do it. What am I missing? Do I need a redirect? If so, where?
I'd like to leave the original page intact/untouched.

Here's my code snippet. Assume "myprog.php" has all the proper sense to send
the right HTTP headers and such.

<script language="JavaScript">

function dlFile(file)
{
var win
=window.open(file,'dlnow','toolbar=0,location=no,directories=0,status=0,
scrollbars=no,resizable=yes,width=1,height=1,top=0,left=0');
win.focus();
// win.close(); // Causes the program to die before the DL starts
}
var dlnl = "dlFile('http://myserver/myprog.php?Open=127967')";
document.write('<a href="javascript:' + dlnl + '">Download</a>');
</script>
 
H

Henri

I had had the same problem as you and I haven't found any solution so far.
If you know that the type of the file that's going to be downloaded is not a
type that can be displayed inside any browser (unlike htm, txt or pdf), a
simple link inside your original page will work.
The problem is with .doc files, IE displays them inside the browser, Mozilla
doesn't...
So the best way is still to open a new window like you did and let the user
close it if the downloaded file doesn't get displayed inside it.

Henri
 
C

Chuck Tomasi

The file is XML and unfortunately a lot of browsers like to DISPLAY XML
with a
simple link so the window.open, run the PHP program that generate the
XML
(with proper HTTP headers) is currently my only option to leave the
original
window intact. My JS code is based largely upon that used by
download.com,
however their download() function uses a couple additional args for
redirection. They send you to one of those "Your download should begin
automatically pages..." I'd rather not do that since I'm only trying to
download an XML synpopsis of the page displayed. I use this in an
issue
tracking system and the XML eventually goes to a 'task manager' type
program.
<p>

One would think that downloading a file shouldn't be this hard with the
years
HTML/JS have had to mature.
 
G

Grant Wagner

Chuck said:

You shouldn't be trying to do this through client-side JavaScript at all. In
fact, you don't need to open a new window to protect the current page. Have
myprog.php output the appropriate Content-Disposition header to avoid having the
browser process the file (even if it knows how). You mention the files are XML?
If you always want to force the download, the simplest thing to do is tell the
browser it's a binary file, then no browser should ever try to interpret the
content:

<a href="myprog.php?Open=127967">Download</a>

Then myprog.php does something like:

<?php

# obtain the information about the file referred to by 'Open'
# including it's name and it's size
# $the_file_name = {the name};
# $the_file_size = filesize($the_file_name);

# to absolutely force almost all browsers to want to save the file
header("Content-Type: application/octet-stream");
# or if you want the browser to properly identify the XML file type
# (you run the risk that some little-used browser will want to parse the file
itself)
# header("Content-Type: text/xml");
header("Content-Disposition: attachment; filename=$the_file_name");
header("Content-Length: $the_file_size");

# open, read and print the file to the client
if ($fp = fopen($the_file_name, "r")) {
while (!feof($fp)) {
$buffer = fgets($fp, 1024);
print "$buffer";
}
fclose($fp);
}
# or if the files aren't too large
# if ($fp = fopen($listFile, "r")) {
# $buffer = fread($fp, filesize($listFile));
# print "$buffer";
# fclose($fp);
# }

?>

Now, clicking the link will present the user with a Save As dialog in almost all
browsers and the current page they are viewing will not be replaced or reloaded.
The above will present a Save As dialog in IE 4+, all recent Gecko-based
browsers, Opera 6+, etc. When I refer to browsers it may not work in, I'm
talking about "Sam's Favorite Home Written Browser" run by 3 people on the
planet (although there may be slightly more popular browsers that do not
properly support Content-Disposition as well).

No reliance on client-side technology at all.
 
C

Chuck Tomasi

That's exactly what I needed. I just tried it (with no modifications to the
PHP code) and it worked. Thanks. I thought I had tried this approach before
and failed which is why I went to client side JS.

--Chuck
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top