How do I send HTML and a file for 'Save-As' at the same time?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • edykstra@virtualcad.com

    How do I send HTML and a file for 'Save-As' at the same time?

    Hello,

    If you point your browser to this page,



    the server sends you the HTML page, and very soon afterwards, you are
    prompted to do a 'Save-As' for a file to download.

    How do they do that without the classic "Headers already sent" error?

    In my application, I need to do something very similar.

    Thanks in advance for any help!

    Eric

  • Daedalus.OS

    #2
    Re: How do I send HTML and a file for 'Save-As' at the same time?

    If you look at the html code of the page you're talking about, you'll notice
    the following:

    <META HTTP-EQUIV="refresh" content="5;
    URL=http://umn.dl.sourcefo rge.net/sourceforge/vnc-tight/tightvnc-1.2.9_x86_viewe r.zip">

    This line makes the file to download 5 seconds after the page is loaded.
    This is often used for redirection, but since in this case it refresh (or
    redirect) to a zip file, then this doesn't replace the page but prompt to
    save the file instead.

    Dae


    <edykstra@virtu alcad.com> wrote in message
    news:1117327839 .547641.216760@ o13g2000cwo.goo glegroups.com.. .[color=blue]
    > Hello,
    >
    > If you point your browser to this page,
    >
    > http://prdownloads.sourceforge.net/v...use_mirror=umn
    >
    > the server sends you the HTML page, and very soon afterwards, you are
    > prompted to do a 'Save-As' for a file to download.
    >
    > How do they do that without the classic "Headers already sent" error?
    >
    > In my application, I need to do something very similar.
    >
    > Thanks in advance for any help!
    >
    > Eric
    >[/color]


    Comment

    • edykstra@virtualcad.com

      #3
      Re: How do I send HTML and a file for 'Save-As' at the same time?

      Hello,

      OK .. that makes sense. I didn't realize it was just waiting 5 seconds
      to serve me a file that already exists. I thought it was processing a
      file and serving it to me as soon as it was ready, which is what I need
      to do.

      The file I am going to serve the User can take anywhere from 3 to 30
      seconds to create dynamically. If I use a value too low, the page will
      refresh to 'Object not found'. If the value is too high, the User is
      waiting longer than they should have to.

      Here is what I want my application to do:

      1. User clicks on a link.
      2. Server sends HTML page explaining it may take up to 30 seconds.
      3. Server sends file prompting a 'Save-As' as soon as it is ready.

      I would like it better if there was no User interaction other than step
      1.

      Is this possible?

      Eric

      Comment

      • Daedalus.OS

        #4
        Re: How do I send HTML and a file for 'Save-As' at the same time?

        Instead of refreshing to a static files, refresh to a php file that will
        send back the file.

        Lets say when the user click the link it brings him to some_page.php. In
        this page you would put this code:

        <META HTTP-EQUIV="refresh" content="1;
        URL=http://yoursite.com/some/folder/file_gen.php">

        file_gen.php should now generate the file and sent it back to be downloaded.
        Without more detail about this generated file I can't tell you how exactly
        to send it back. But here is a simple exemple that would return a
        dynamically created javasctipt to be downloaded:

        // The file is generated and its handler is contained by $file...
        header('Content-type: application/javascript');
        header("Content-length: ".filesize($fil e));
        header('Content-Disposition: attachment; filename="desti nation_name.js" ');
        readfile($file) ;

        You will find some more exemple in the user comments of the header() and
        fread() manual pages:



        Other function that might be useful:




        Dae

        <edykstra@virtu alcad.com> wrote in message
        news:1117333937 .281656.130930@ g44g2000cwa.goo glegroups.com.. .[color=blue]
        > Hello,
        >
        > OK .. that makes sense. I didn't realize it was just waiting 5 seconds
        > to serve me a file that already exists. I thought it was processing a
        > file and serving it to me as soon as it was ready, which is what I need
        > to do.
        >
        > The file I am going to serve the User can take anywhere from 3 to 30
        > seconds to create dynamically. If I use a value too low, the page will
        > refresh to 'Object not found'. If the value is too high, the User is
        > waiting longer than they should have to.
        >
        > Here is what I want my application to do:
        >
        > 1. User clicks on a link.
        > 2. Server sends HTML page explaining it may take up to 30 seconds.
        > 3. Server sends file prompting a 'Save-As' as soon as it is ready.
        >
        > I would like it better if there was no User interaction other than step
        > 1.
        >
        > Is this possible?
        >
        > Eric
        >[/color]


        Comment

        • edykstra@virtualcad.com

          #5
          Re: How do I send HTML and a file for 'Save-As' at the same time?

          Dae,

          Thanks! I came up with the same idea late last night while I was trying
          to get to sleep. What I will do is refresh the page over and over until
          the file is ready. On each refresh, I can show a 'progress bar'. When
          the file is ready, the following refresh will send the file.

          Eric

          Comment

          • Daedalus.OS

            #6
            Re: How do I send HTML and a file for 'Save-As' at the same time?

            I don't understand why you want to refresh over and over ? If you create
            your file within the file_gen.php file (from my previous exemple) and then
            when the creation process is finished make the script sends the required
            headers followed by fread('file') ... when you will do the refresh to that
            file the browser should wait for the headers before taking any action (as
            long as the browser request doesn't timed out). When it will receive it
            since the header will specify Content-Disposition: attachment;
            filename="desti nation_file_nam e.ext", then it should prompt to save the
            file.

            Dae


            <edykstra@virtu alcad.com> wrote in message
            news:1117368506 .468500.139130@ g47g2000cwa.goo glegroups.com.. .[color=blue]
            > Dae,
            >
            > Thanks! I came up with the same idea late last night while I was trying
            > to get to sleep. What I will do is refresh the page over and over until
            > the file is ready. On each refresh, I can show a 'progress bar'. When
            > the file is ready, the following refresh will send the file.
            >
            > Eric
            >[/color]


            Comment

            • John Dunlop

              #7
              Re: How do I send HTML and a file for 'Save-As' at the same time?

              Daedalus.OS wrote:
              [color=blue]
              > Content-Disposition: attachment; filename="desti nation_file_nam e.ext"[/color]

              do browsers on the whole take heed of that line do you know?

              --
              Jock

              Comment

              • Daedalus.OS

                #8
                Re: How do I send HTML and a file for 'Save-As' at the same time?

                This should work in all recent browser. Content-Disposition is stated as
                experimental but is implemented in most (if not all) major browser.

                I have tested it with IE6, FireFox 1.0, Netscape7.02, Opera7.5.4,
                Mozilla1.7.3
                I know there were issues on some browser that would result in displaying the
                file if the content-type was a known text type or that would download the
                file with an php or html extension.

                I don't remenber wich browser and version have wich issue (I think IE5.5 was
                one of those and FireFox 0.9 was the other), but I remember I was using
                application/octet-stream as content-type for text file to solve this.

                Dae


                "John Dunlop" <usenet+2004@jo hn.dunlop.name> wrote in message
                news:MPG.1d0415 903871ab8b98968 8@news.ntlworld .com...[color=blue]
                > Daedalus.OS wrote:
                >[color=green]
                >> Content-Disposition: attachment; filename="desti nation_file_nam e.ext"[/color]
                >
                > do browsers on the whole take heed of that line do you know?
                >
                > --
                > Jock[/color]


                Comment

                • edykstra@virtualcad.com

                  #9
                  Re: How do I send HTML and a file for 'Save-As' at the same time?

                  Dae,

                  Without a request from the client for another 'page' won't I receive
                  the 'Headers already sent' error?

                  Eric

                  Daedalus.OS wrote:[color=blue]
                  > I don't understand why you want to refresh over and over ? If you create
                  > your file within the file_gen.php file (from my previous exemple) and then
                  > when the creation process is finished make the script sends the required
                  > headers followed by fread('file') ... when you will do the refresh to that
                  > file the browser should wait for the headers before taking any action (as
                  > long as the browser request doesn't timed out). When it will receive it
                  > since the header will specify Content-Disposition: attachment;
                  > filename="desti nation_file_nam e.ext", then it should prompt to save the
                  > file.
                  >
                  > Dae
                  >
                  >
                  > <edykstra@virtu alcad.com> wrote in message
                  > news:1117368506 .468500.139130@ g47g2000cwa.goo glegroups.com.. .[color=green]
                  > > Dae,
                  > >
                  > > Thanks! I came up with the same idea late last night while I was trying
                  > > to get to sleep. What I will do is refresh the page over and over until
                  > > the file is ready. On each refresh, I can show a 'progress bar'. When
                  > > the file is ready, the following refresh will send the file.
                  > >
                  > > Eric
                  > >[/color][/color]

                  Comment

                  • Ivan Omelchenko 608308824

                    #10
                    Re: How do I send HTML and a file for 'Save-As' at the same time?

                    Daedalus.OS ÐÉÛÅÔ:[color=blue]
                    > Instead of refreshing to a static files, refresh to a php file that will
                    > send back the file.
                    >
                    > Lets say when the user click the link it brings him to some_page.php. In
                    > this page you would put this code:
                    >
                    > <META HTTP-EQUIV="refresh" content="1;
                    > URL=http://yoursite.com/some/folder/file_gen.php">
                    >
                    > file_gen.php should now generate the file and sent it back to be downloaded.
                    > Without more detail about this generated file I can't tell you how exactly
                    > to send it back. But here is a simple exemple that would return a
                    > dynamically created javasctipt to be downloaded:
                    >
                    > // The file is generated and its handler is contained by $file...
                    > header('Content-type: application/javascript');
                    > header("Content-length: ".filesize($fil e));
                    > header('Content-Disposition: attachment; filename="desti nation_name.js" ');
                    > readfile($file) ;
                    >[/color]
                    Hows about $file that weight more then 8Mb ?
                    You can't do it with movie, for example.

                    Comment

                    • Daniel Tryba

                      #11
                      Re: How do I send HTML and a file for 'Save-As' at the same time?

                      In comp.lang.php Ivan Omelchenko 608308824 <news@omelchenk o.com> wrote:[color=blue][color=green]
                      >> // The file is generated and its handler is contained by $file...
                      >> header('Content-type: application/javascript');
                      >> header("Content-length: ".filesize($fil e));
                      >> header('Content-Disposition: attachment; filename="desti nation_name.js" ');
                      >> readfile($file) ;
                      >>[/color]
                      > Hows about $file that weight more then 8Mb ?
                      > You can't do it with movie, for example.[/color]

                      Why do you think that? readfile() "Reads a file and writes it to the
                      output buffer." [http://nl3.php.net/readfile]. So it doesn't use any
                      memory in php other than:
                      -the buffers to read the file and write to the output buffer.
                      -the output buffer _if_ output buffering in php is set to on, not
                      something you want to do in these kind of scripts...

                      Comment

                      • Daedalus.OS

                        #12
                        Re: How do I send HTML and a file for 'Save-As' at the same time?

                        > Dae,[color=blue]
                        >
                        > Without a request from the client for another 'page' won't I receive
                        > the 'Headers already sent' error?
                        >
                        > Eric[/color]

                        No. The thing here is that since you're calling it as a new page with a
                        <META HTTP-EQUIV="refresh" ...>, the browser is actually waiting to receive a
                        totally new page with new headers. All what the page you call have to do is
                        making the file, send the proper header and the file. Since you're making it
                        as a Content-Disposition: attachment, the new page will not replace the
                        actual page in the browser but prompt to save it to disk instead.

                        So here is a simple view of what is happenning when a user click the link to
                        some_page.php:
                        -The browser receive the header for that page and display the page.
                        -Then the meta http-equiv="refresh" ... attempt to refresh to file_gen.php
                        -The browser think he is going to another page and wait to receive the
                        headers
                        -file_gen.php create the file and then sends the headers + the file
                        -Browser receives the headers and see the Content-Disposition: attachment,
                        this mean (for the browser) do not display, prompt for download.

                        (This is a simplified view ... of course)

                        Dae


                        Comment

                        • edykstra@virtualcad.com

                          #13
                          Re: How do I send HTML and a file for 'Save-As' at the same time?

                          Hello Dae,

                          I get it now. However, as I have worked through this application, I
                          have decided on more functionality that adds to the 'requirements' of
                          the application. Basically, I need a Javascript 'Progress Bar' to let
                          the User know things are working. I also need to partially control the
                          progress bar from the server side. What I plan to do is simply use a
                          client side progress bar (so it is really a 'guesstimate' of progress)
                          and control from the server side if it is displayed or not.

                          Here is the flow I see before I added this requirement:

                          1. User clicks on link for some_page.php which loads.
                          2. Refresh in some_page.php asks for file_gen.php which starts creating
                          a file.
                          3. While user is waiting, the 'progress bar' on some_page.php begins.
                          4. file_gen.php completes file and sends to client.
                          5. User does a 'Save-As' (or Open-With etc).
                          6. some_page.php is still open and progress bar is still executing.

                          This is misleading since the progress is complete.

                          Here is what I propose, which minimizes the refreshes, but still allows
                          control over the progress bar. Let me know if this makes sense.

                          Better method:

                          1. User clicks on link for some_page.php which loads, with no progress
                          bar.
                          2. User specifies file creation parameters and clicks submit (manual
                          refresh).
                          3. Server sends back some_page.php with Javascript progress bar
                          started.
                          4. Refresh in some_page.php asks for file_gen.php which starts
                          creating file.
                          5. While User is waiting, the 'progress bar' on some_page.php
                          continues.
                          6. file_gen.php completes file and sends same some_page.php to client.
                          7. some_page.php now has a progress bar at 100%.
                          8. some_page.php also contains a refresh to a now existing file.
                          9. Page refreshes and User does a 'Save-As' (or Open-With etc).
                          10. some_page.php is still open with progress bar at 100%.
                          11. User can go back to step 2, which causes the progress bar to be
                          removed.

                          Note: The 'some_page.php' is actually a small dialog with only 2 small
                          jpegs. The file to be created can take up to 30 seconds on a FAST
                          machine. (Long story) If either of these facts were not true, I
                          wouldn't really like the above solution. But, I think in this case it
                          is the best solution.

                          Thoughts? Have I 'got it' now? :)

                          Thanks,

                          Eric



                          Daedalus.OS wrote:[color=blue][color=green]
                          > > Dae,
                          > >
                          > > Without a request from the client for another 'page' won't I receive
                          > > the 'Headers already sent' error?
                          > >
                          > > Eric[/color]
                          >
                          > No. The thing here is that since you're calling it as a new page with a
                          > <META HTTP-EQUIV="refresh" ...>, the browser is actually waiting to receive a
                          > totally new page with new headers. All what the page you call have to do is
                          > making the file, send the proper header and the file. Since you're making it
                          > as a Content-Disposition: attachment, the new page will not replace the
                          > actual page in the browser but prompt to save it to disk instead.
                          >
                          > So here is a simple view of what is happenning when a user click the link to
                          > some_page.php:
                          > -The browser receive the header for that page and display the page.
                          > -Then the meta http-equiv="refresh" ... attempt to refresh to file_gen.php
                          > -The browser think he is going to another page and wait to receive the
                          > headers
                          > -file_gen.php create the file and then sends the headers + the file
                          > -Browser receives the headers and see the Content-Disposition: attachment,
                          > this mean (for the browser) do not display, prompt for download.
                          >
                          > (This is a simplified view ... of course)
                          >
                          > Dae[/color]

                          Comment

                          • Kimmo Laine

                            #14
                            Re: How do I send HTML and a file for 'Save-As' at the same time?

                            <edykstra@virtu alcad.com> kirjoitti
                            viestissä:11174 86474.870158.88 600@g14g2000cwa .googlegroups.c om...[color=blue]
                            > Hello Dae,
                            >
                            > I get it now. However, as I have worked through this application, I
                            > have decided on more functionality that adds to the 'requirements' of
                            > the application. Basically, I need a Javascript 'Progress Bar' to let
                            > the User know things are working. I also need to partially control the
                            > progress bar from the server side. What I plan to do is simply use a
                            > client side progress bar (so it is really a 'guesstimate' of progress)
                            > and control from the server side if it is displayed or not.
                            >
                            > Here is the flow I see before I added this requirement:
                            >
                            > 1. User clicks on link for some_page.php which loads.
                            > 2. Refresh in some_page.php asks for file_gen.php which starts creating
                            > a file.
                            > 3. While user is waiting, the 'progress bar' on some_page.php begins.
                            > 4. file_gen.php completes file and sends to client.
                            > 5. User does a 'Save-As' (or Open-With etc).
                            > 6. some_page.php is still open and progress bar is still executing.
                            >
                            > This is misleading since the progress is complete.
                            >
                            > Here is what I propose, which minimizes the refreshes, but still allows
                            > control over the progress bar. Let me know if this makes sense.
                            >
                            > Better method:
                            >
                            > 1. User clicks on link for some_page.php which loads, with no progress
                            > bar.
                            > 2. User specifies file creation parameters and clicks submit (manual
                            > refresh).
                            > 3. Server sends back some_page.php with Javascript progress bar
                            > started.
                            > 4. Refresh in some_page.php asks for file_gen.php which starts
                            > creating file.
                            > 5. While User is waiting, the 'progress bar' on some_page.php
                            > continues.
                            > 6. file_gen.php completes file and sends same some_page.php to client.
                            > 7. some_page.php now has a progress bar at 100%.
                            > 8. some_page.php also contains a refresh to a now existing file.
                            > 9. Page refreshes and User does a 'Save-As' (or Open-With etc).
                            > 10. some_page.php is still open with progress bar at 100%.
                            > 11. User can go back to step 2, which causes the progress bar to be
                            > removed.
                            >
                            > Note: The 'some_page.php' is actually a small dialog with only 2 small
                            > jpegs. The file to be created can take up to 30 seconds on a FAST
                            > machine. (Long story) If either of these facts were not true, I
                            > wouldn't really like the above solution. But, I think in this case it
                            > is the best solution.
                            >
                            > Thoughts? Have I 'got it' now? :)
                            >[/color]


                            You 'got it' up to the point where you said 'I get it now'. Then you started
                            rambling about this crazy progressbar... Look, you can say to user
                            "generating file may take up to 30 seconds, please wait". And there's a
                            progressbar in all browsers I've seen so far. Call me old-fashioned, but I
                            think a) the users aren't that stupid that they would require another
                            progressbar and b) you'll just end up looking stupid with the thing.

                            Just a thought.

                            --
                            "I am pro death penalty. That way people learn
                            their lesson for the next time." -- Britney Spears

                            eternal.erectio nN0@5P4Mgmail.c om


                            Comment

                            • Daedalus.OS

                              #15
                              Re: How do I send HTML and a file for 'Save-As' at the same time?

                              Well I think it would work, but whether it make sense or not depends how bad
                              you want this progress bar. Personnaly I have to admit that I tend to agree
                              (in a more diplomatic way maybe) with kimmo. I don't see the use of a
                              progress bar since all browser shows its own page progress bar or elements
                              download status. Adding a message in this case would be as efficient IMHO as
                              a fake progress bar ... But still, I think it would work.

                              Dae



                              "Kimmo Laine" <eternal.erecti onN0.5P@Mgmail. com> wrote in message
                              news:d7gnk5$452 $1@phys-news1.kolumbus. fi...[color=blue]
                              > <edykstra@virtu alcad.com> kirjoitti
                              > viestissä:11174 86474.870158.88 600@g14g2000cwa .googlegroups.c om...[color=green]
                              >> Hello Dae,
                              >>
                              >> I get it now. However, as I have worked through this application, I
                              >> have decided on more functionality that adds to the 'requirements' of
                              >> the application. Basically, I need a Javascript 'Progress Bar' to let
                              >> the User know things are working. I also need to partially control the
                              >> progress bar from the server side. What I plan to do is simply use a
                              >> client side progress bar (so it is really a 'guesstimate' of progress)
                              >> and control from the server side if it is displayed or not.
                              >>
                              >> Here is the flow I see before I added this requirement:
                              >>
                              >> 1. User clicks on link for some_page.php which loads.
                              >> 2. Refresh in some_page.php asks for file_gen.php which starts creating
                              >> a file.
                              >> 3. While user is waiting, the 'progress bar' on some_page.php begins.
                              >> 4. file_gen.php completes file and sends to client.
                              >> 5. User does a 'Save-As' (or Open-With etc).
                              >> 6. some_page.php is still open and progress bar is still executing.
                              >>
                              >> This is misleading since the progress is complete.
                              >>
                              >> Here is what I propose, which minimizes the refreshes, but still allows
                              >> control over the progress bar. Let me know if this makes sense.
                              >>
                              >> Better method:
                              >>
                              >> 1. User clicks on link for some_page.php which loads, with no progress
                              >> bar.
                              >> 2. User specifies file creation parameters and clicks submit (manual
                              >> refresh).
                              >> 3. Server sends back some_page.php with Javascript progress bar
                              >> started.
                              >> 4. Refresh in some_page.php asks for file_gen.php which starts
                              >> creating file.
                              >> 5. While User is waiting, the 'progress bar' on some_page.php
                              >> continues.
                              >> 6. file_gen.php completes file and sends same some_page.php to client.
                              >> 7. some_page.php now has a progress bar at 100%.
                              >> 8. some_page.php also contains a refresh to a now existing file.
                              >> 9. Page refreshes and User does a 'Save-As' (or Open-With etc).
                              >> 10. some_page.php is still open with progress bar at 100%.
                              >> 11. User can go back to step 2, which causes the progress bar to be
                              >> removed.
                              >>
                              >> Note: The 'some_page.php' is actually a small dialog with only 2 small
                              >> jpegs. The file to be created can take up to 30 seconds on a FAST
                              >> machine. (Long story) If either of these facts were not true, I
                              >> wouldn't really like the above solution. But, I think in this case it
                              >> is the best solution.
                              >>
                              >> Thoughts? Have I 'got it' now? :)
                              >>[/color]
                              >
                              >
                              > You 'got it' up to the point where you said 'I get it now'. Then you
                              > started rambling about this crazy progressbar... Look, you can say to user
                              > "generating file may take up to 30 seconds, please wait". And there's a
                              > progressbar in all browsers I've seen so far. Call me old-fashioned, but I
                              > think a) the users aren't that stupid that they would require another
                              > progressbar and b) you'll just end up looking stupid with the thing.
                              >
                              > Just a thought.
                              >
                              > --
                              > "I am pro death penalty. That way people learn
                              > their lesson for the next time." -- Britney Spears
                              >
                              > eternal.erectio nN0@5P4Mgmail.c om
                              >[/color]


                              Comment

                              Working...