Lingering Window

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chuck Tomasi

    Lingering Window

    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="JavaS cript">

    function dlFile(file)
    {
    var win
    =window.open(fi le,'dlnow','too lbar=0,location =no,directories =0,status=0,
    scrollbars=no,r esizable=yes,wi dth=1,height=1, top=0,left=0');
    win.focus();
    // win.close(); // Causes the program to die before the DL starts
    }
    var dlnl = "dlFile('ht tp://myserver/myprog.php?Open =127967')";
    document.write( '<a href="javascrip t:' + dlnl + '">Download</a>');
    </script>



  • Henri

    #2
    Re: Lingering Window

    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


    "Chuck Tomasi" <ctomasi@new.rr .com> a écrit dans le message de
    news:ZVwnd.10$9 z.5@twister.rdc-kc.rr.com...[color=blue]
    > I'm clearly missing something and I've been looking for a solution for[/color]
    hours[color=blue]
    > without a clue.
    >
    > Objective: Create a typical "download" link using JS window.open. I get[/color]
    the[color=blue]
    > window to open, the "Save As" box appears, and the file ends up where I[/color]
    want[color=blue]
    > 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,[/color]
    but[color=blue]
    > that doesn't do it. What am I missing? Do I need a redirect? If so,[/color]
    where?[color=blue]
    > I'd like to leave the original page intact/untouched.
    >
    > Here's my code snippet. Assume "myprog.php " has all the proper sense to[/color]
    send[color=blue]
    > the right HTTP headers and such.
    >
    > <script language="JavaS cript">
    >
    > function dlFile(file)
    > {
    > var win
    > =window.open(fi le,'dlnow','too lbar=0,location =no,directories =0,status=0,
    > scrollbars=no,r esizable=yes,wi dth=1,height=1, top=0,left=0');
    > win.focus();
    > // win.close(); // Causes the program to die before the DL starts
    > }
    > var dlnl = "dlFile('ht tp://myserver/myprog.php?Open =127967')";
    > document.write( '<a href="javascrip t:' + dlnl + '">Download</a>');
    > </script>
    >
    >
    >
    >[/color]



    Comment

    • Chuck Tomasi

      #3
      Re: Lingering Window


      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.

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Grant Wagner

        #4
        Re: Lingering Window

        Chuck Tomasi wrote:
        [color=blue]
        > var dlnl = "dlFile('ht tp://myserver/myprog.php?Open =127967')";[/color]

        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.ph p?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_f ile_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_f ile_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($listF ile));
        # 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.

        --
        Grant Wagner <gwagner@agrico reunited.com>
        comp.lang.javas cript FAQ - http://jibbering.com/faq

        Comment

        • Chuck Tomasi

          #5
          Re: Lingering Window

          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

          "Grant Wagner" <gwagner@agrico reunited.com> wrote in message
          news:41A21511.A 92C7BE6@agricor eunited.com...[color=blue]
          > Chuck Tomasi wrote:
          >[color=green]
          > > var dlnl = "dlFile('ht tp://myserver/myprog.php?Open =127967')";[/color]
          >
          > You shouldn't be trying to do this through client-side JavaScript at all.[/color]
          In[color=blue]
          > fact, you don't need to open a new window to protect the current page.[/color]
          Have[color=blue]
          > myprog.php output the appropriate Content-Disposition header to avoid[/color]
          having the[color=blue]
          > browser process the file (even if it knows how). You mention the files are[/color]
          XML?[color=blue]
          > If you always want to force the download, the simplest thing to do is tell[/color]
          the[color=blue]
          > browser it's a binary file, then no browser should ever try to interpret[/color]
          the[color=blue]
          > content:
          >
          > <a href="myprog.ph p?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_f ile_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[/color]
          file[color=blue]
          > itself)
          > # header("Content-Type: text/xml");
          > header("Content-Disposition: attachment; filename=$the_f ile_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($listF ile));
          > # print "$buffer";
          > # fclose($fp);
          > # }
          >
          > ?>
          >
          > Now, clicking the link will present the user with a Save As dialog in[/color]
          almost all[color=blue]
          > browsers and the current page they are viewing will not be replaced or[/color]
          reloaded.[color=blue]
          > 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.
          >
          > --
          > Grant Wagner <gwagner@agrico reunited.com>
          > comp.lang.javas cript FAQ - http://jibbering.com/faq
          >[/color]


          Comment

          Working...