0

I want to show some kind of notification based on my file download status like it's failed or success. what's the actual problem here it is when I'm clicking download button it's opening default OS popup window like where to save the downloaded file and I'm triggering below method's code on the click of download button and it's showing notification of success before downloading a file. I could not able to do find any resource that can me help out to find the status of downloading of a file

Below is the code of downloading a file

   const type = 'text/json';
      const fileName = `${programName}.json`;
      const data = JSON.stringify(json);
      const blob = new Blob([data], { type });
      // Create an anchor element and dispatch a click event on it
      // to trigger a download
      const a = document.createElement('a');
      a.download = fileName;
      a.href = URL.createObjectURL(blob);
      const clickEvt = new MouseEvent('click', {
        view: window,
        bubbles: true,
        cancelable: true,
      });
      a.dispatchEvent(clickEvt);
      a.remove();
      setOpen(true);  // here I'm showing notification

I've tried to show the notification once the file is downloaded I want to show the notification only after the file is either download is success or failure or else if I can listen the save or cancel button event of os window file saver in my code that would also work. It's actually showing the notification before downloading the file because of OS window popup where to save the file

  • You can look at this answer on another similar question: https://stackoverflow.com/a/44814881/5793132 Basically, you need to download the file with `XMLHttpRequest`, show progress, status etc, and create a link to that downloaded file with [`URL.createObjectURL`](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL) – Ugur Eren Nov 08 '22 at 12:47
  • 2
    _"I could not able to do find any resource that can me help out to find the status of downloading of a file"_ - there is no such thing. With a download from the server, you could at least somehow check, how many bytes you've already sent out or received, and what the status of the connection is. But there is no server involved here, your download is purely client-side. – CBroe Nov 08 '22 at 12:47

0 Answers0