Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a very simple and standard PHP force download script.

How do I check if/when the download has completed in order to notify the user on the clientside? I don't even need to show the progress in real time, I am only interested in the very specific event: "when the download completes". Based on my research, it seems like it would have to be determined from the serverside as there is noondownloadready event and I don't think it is possible to intercept browser events.

So it seems that my best bet would be to compare bytes sent to total bytes with some sort of clientside/severside interaction. How would I go about checking the bytes sent from the server for a PHP forced download? is there some sort of global PHP variable that store these data that I can ping with AJAX?

    <?php

    header("Content-Type: video/x-msvideo");
    header("Content-Disposition: attachment; filename="".basename($realpath)."";");

    ...

    $chunksize = 1 * (1024 * 1024); // how many bytes per chunk
    if ($size > $chunksize) {
           $handle = fopen($realpath, 'rb');
           $buffer = '';
           while (!feof($handle)) {
                 $buffer = fread($handle, $chunksize);
                 echo $buffer;
                 ob_flush();
                 flush();
           }
          fclose($handle);
     }             
     else {
         readfile($realpath);
     }
     exit();
     ?>

The reason I need this:

For the project I am working on, it is required that after the download starts, the page redirects to (or displays) a "please wait while the download completes" page. Then, once it is complete, it should redirect to (or display) a "Your download is complete, thank you" page. I am open to other ideas that would achieve the same result.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
566 views
Welcome To Ask or Share your Answers For Others

1 Answer

Check out this Sitepoint Forum Post that describes the solution.

Basically, once the while loop breaks, you're done!

Here's the full thread that describes using an AJAX poll to detect when the download is complete: http://www.sitepoint.com/forums/showthread.php?t=618233


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...