detecting when the “File download” popup is closed

后端 未结 5 1733
情书的邮戳
情书的邮戳 2020-11-28 11:53

I have a web page (made with JSF) where some links allow the user to get a PDF file.

When the user clicks on such a link, a waiting popup (it is a modal panel) is di

5条回答
  •  醉梦人生
    2020-11-28 12:34

    All of the answers that I read here and in related question on stackoverflow and elsewhere only solve the first part of the problem, the time it takes for the server to prepare the file. The second part of the problem, which is the time it takes for the file to actually finish downloading on the client, is not so trivial.

    In our application we followed the following approach. We already have a notification push mechanism based on cometd (you can read more about cometd here: What is Cometd ? Why it is used and how to work on that), but I suppose you could also use WebSockets or something similar. We use Java in the back-end, but it can be anything. So:

    1. Client initiates download sending a flag/token containing the user ID and some other unique value that is application and download specific. It hides the download button.
    2. Server receives the request, prepares the file for download and starts the response, setting appropriate headers as discussed in other solutions (e.g. using a cookie).
    3. Client receives the response, but does not show the button, it just knows that the server has successfully created the file and the download started.
    4. Server knows when the download actually finishes (because it's in a loop streaming the file to the output, as described here: https://stackoverflow.com/a/2343483/134120). When the server knows that the file has finished downloading, it pushes a notification to the client with the token from step 1 using cometd.
    5. Client receives notification that the download finished and shows the button again.

提交回复
热议问题