开发者

how to know whether file is downloaded by user or not?

开发者 https://www.devze.com 2022-12-25 06:58 出处:网络
I have a functionality where a user is given file to download. It works fine. BUT - How to know whether user has downloaded a file or cancelled it?

I have a functionality where a user is given file to download. It works fine. BUT -

  1. How to know whether user has downloaded a file or cancelled it?
  2. After downloading I wat to redirect current page to another one that is also not happening.

Please let me know if any one 开发者_如何转开发of you have any idea about it. (I am doing it in Joomla)

Regards, Shahu


It depends a bit on how much trouble you want to go to.

The only way to know whether they actually downloaded the file is to hook into the act of transmitting the file to the client — so that means either hooking into Joomla or whatever underlying web server you're using, or making the download go through a PHP page that basically just reads the file from disk and sends it out. Even if you do that, though, all you really know is that the file was requested by the browser and that you satisified the request. You don't know that the user didn't throw it away immediately afterward.

In terms of showing a redirect when they're done, if you've hooked into the process as per the above, you could set a session variable flagging whether the file got downloaded (e.g., after sending the last byte). You could use Ajax to periodically query the status of that session flag, and use JavaScript to do the redirect on the client if the flag came back true.

If all you need to know is that they started the download, you can do that with a cookie mechanism. When sending back the file, add a cookie to the response. (You might do this by funnelling the download through a PHP file as per the above, or if your web server is highly configurable — Apache could do this, for instance — you could configure it to automatically add the cookie.) Your page waiting to see if they downloaded the file (or at least, started to download it) uses JavaScript to periodically check for the cookie (rather than polling the server), which is less traffic. This question and answer talks about doing that in a (little) bit more detail. I do this in an app where the server is custom-building a large PDF or ZIP for download: I show a "building" div when they request the file, then poll for the cookie, and remove the "building" div when the cookie shows up. In the major browsers, the cookie shows up at exactly the moment the browser shows the box asking whether to save the file to disk or open it, because both are triggered by the response coming back.

If the above is too much hassle (and for some use cases, I think it probably would be), you can put a JavaScript click event handler on the download link. That would tell you that they clicked the link, but doesn't really tell you much else.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号