Home > Web Front-end > JS Tutorial > How Can We Reliably Detect Browser File Download Completion Without Browser Events?

How Can We Reliably Detect Browser File Download Completion Without Browser Events?

Barbara Streisand
Release: 2024-12-23 21:11:14
Original
441 people have browsed it

How Can We Reliably Detect Browser File Download Completion Without Browser Events?

Detecting Browser File Download Completion

Problem Statement

A page featuring a dynamic file download mechanism requires a visual indicator to signal when the download completes. However, browsers do not always trigger events to indicate file reception when a "Save" dialog appears.

Server Response Limitations

The provision of a "Content-Disposition: attachment" header instructs browsers to display the "Save" dialog, but it suppresses the loading of the iframe used for event monitoring.

Proposed Solution

Implement a client-based solution leveraging JavaScript and server-side cookies:

Client Algorithm

  1. Generate a unique token.
  2. Submit the download request with the token.
  3. Display the "waiting" indicator.
  4. Poll the server at regular intervals for a cookie named "fileDownloadToken" that matches the token.
  5. If the cookie is found, remove the "waiting" indicator.

Server Algorithm

  1. Extract the token from the request.
  2. Create a cookie with the name "fileDownloadToken" and set its value to the received token.

Example Implementation

  • Client-Side JavaScript (simplified)
  • Server-Side PHP (simplified)

Benefits

  • Does not require temporary file creation.
  • Detects file download readiness without relying on event listeners.
  • Can be customized to specific token names and cookie settings.

The above is the detailed content of How Can We Reliably Detect Browser File Download Completion Without Browser Events?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template