Detect when browser receives file download

前端 未结 22 1687
陌清茗
陌清茗 2020-11-21 04:55

I have a page that allows the user to download a dynamically-generated file. It takes a long time to generate, so I\'d like to show a \"waiting\" indicator. The problem is,

22条回答
  •  别那么骄傲
    2020-11-21 05:26

    old thread, i know...

    but those, that are lead here by google might be interested in my solution. it is very simple, yet reliable. and it makes it possible to display real progress messages (and can be easily plugged in to existing processes):

    the script that processes (my problem was: retrieving files via http and deliver them as zip) writes the status to the session.

    the status is polled and displayed every second. thats all (ok, its not. you have to take care of a lot of details [eg concurrent downloads], but its a good place to start ;-)).

    the downloadpage:

        DOWNLOAD 1
        DOWNLOAD 2
        ...
        
    Please wait...

    getstatus.php

    
    

    download.php

        "pending","message"=>"Processing".$someinfo);
          session_write_close();
          $processing=do_what_has_2Bdone();
          session_start();
        }
          $_SESSION['downloadstatus']=array("status"=>"finished","message"=>"Done");
    //and spit the generated file to the browser
        ?>
    

提交回复
热议问题