3

I am trying to solve the following problem, but I can't come up with an appropriate solution:

Disabling a button (with an underlying Download link) when the download starts (button has clicked once) and enable the button again when the download is completed?

I tried to Google it but couldn't even find a helpful hint. Is this even possible in general?

Tarabass
  • 3,132
  • 2
  • 17
  • 35
F4k3d
  • 653
  • 1
  • 10
  • 29

1 Answers1

6

3 possible options that you might want to look at.

  1. This link might be useful to you as a place to get started from:

    http://gruffcode.com/2010/10/28/detecting-the-file-download-dialog-in-the-browser/

    The idea is to use a cookie to define when the file is downloaded. In theory you could write your JavaScript to detect the cookie and then re-enable the button. A good outline for detecting a cookie in JavaScript can be found here: What is the shortest function for reading a cookie by name in JavaScript?

  2. Another solution might be to have a look at this question: detecting when the "File download" popup is closed

    While it's not exactly what you're looking for - the answer by Crescent Fresh could probably be adapted so that for the period of the download your button is disabled.

  3. Or a quick but inelegant solution would be to disable the download button for somewhere between 30 seconds and 2 minutes to prevent users clicking the button again immediately while the file downloads - it's the least work, but it doesn't account for different download speeds etc.

Hope that helps!

Community
  • 1
  • 1
hlh3406
  • 1,382
  • 5
  • 29
  • 46
  • Thank you Hollie3406! Your answer helped me to get on the right path. – F4k3d Aug 12 '15 at 10:52
  • 1
    Even though the first link looks old, this is still a very good (and easy) way of implementing this feature. It took around 15 minutes and works perfectly. A few hints to make it even easier: 1) Use js-cookie library, which has replaced the jQuery library (https://github.com/js-cookie/js-cookie/tree/latest#readme) 2) From server-side, simply call: HttpContext.Response.AppendCookie(new HttpCookie("fileDownloadToken", model.FileDownloadToken)); Before returning regular FileContentResult, i.e.: return File(file.Data, mimeType, file.FileName); – Lars Peter Larsen Feb 20 '20 at 08:27