Detect if Download is Complete
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 > $chunksiz开发者_StackOverflow中文版e) {
$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.
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
精彩评论