Handle File Download Completion Detection
Problem:
Detecting when a browser fully receives a dynamically generated file download becomes crucial when displaying waiting indicators. Currently, using an iframe's "load" event fails to address this issue due to browser inconsistencies.
Solution 1: Browser-Based Token Verification
A client-side JavaScript solution involves:
On the server-side, the algorithm:
Code Snippet (JavaScript):
function setFormToken() { var downloadToken = new Date().getTime(); document.getElementById("downloadToken").value = downloadToken; return downloadToken; }
Code Snippet (PHP):
// Set a cookie to unblock submit button when download begins. $TOKEN = "downloadToken"; $this->setCookieToken($TOKEN, $_GET[$TOKEN]);
Solution 2: Server Polling
Alternatively, a server-side approach involves:
This method avoids creating temporary server files. However, it requires client-server communication during the waiting period.
Pros and Cons:
Using a browser token allows for dynamic detection, but may have browser compatibility issues. Server polling is a more reliable but slower option.
The above is the detailed content of How Can I Reliably Detect File Download Completion in a Browser?. For more information, please follow other related articles on the PHP Chinese website!