2

In a small JavaScript web app, I am using

window.location = '[url]/download-file';

to actually download a file as an attachment. On the server side, I'm using Content-Disposition as 'attachment; filename=xyz.xml' This works great and I'm able to trigger a download on the browser without actually reloading the current page (since it's a single page app, I need to keep the current state of the DOM even after the download is finished.)

However, I would like to show a small ajax loader svg while the file is being downloaded. I have no issue with the logic to display/hide the loader. Where I'm struggling is on how to display the loader when the window.location starts, and hide it once it is resolved (when the browser download prompts appear to save the file.)

Is there an event I could listen to in JavaScript to show/hide the loader when the browser http request starts until the browser http request stops?

Loic Duros
  • 5,472
  • 10
  • 43
  • 56
  • 1
    There is no way to do this only on the client. https://stackoverflow.com/questions/2343418/browser-event-when-downloaded-file-is-saved-to-disk – klugjo Dec 12 '17 at 02:24
  • 1
    OK. I had some hope with beforeunload, but I'm guessing this is something entirely different: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload – Loic Duros Dec 12 '17 at 02:26

1 Answers1

0

Since this is not possible, I will use a server side cookie to track whether the response has been sent or not, as explained in the answer of the following stack overflow question: Detect when browser receives file download

Loic Duros
  • 5,472
  • 10
  • 43
  • 56