0

I'm using the following code to trigger a download from a href:

function save(href, download) {
   let element = document.createElement('a');
   element.setAttribute('href', href);
   element.setAttribute('download', download);
   element.style.display = 'none';
   document.body.appendChild(element);
   element.click();
   document.body.removeChild(element);
}

I'd like to listen to the download-event, mainly having a promise around save(), so that I can see whether or not the user clicked on save or cancel in a (resolve, reject) promise mapping.

Any way to do this?

Frame91
  • 3,670
  • 8
  • 45
  • 89
  • download also sets the path: https://www.w3schools.com/tags/att_a_download.asp – Frame91 Oct 17 '17 at 19:52
  • I cannot listen to save/cancel browser-events, can I? The download-dialog is opened by Chrome/Firefox – Frame91 Oct 17 '17 at 19:53
  • 1
    Oh, my bad, I didn't fully get what you were doing here. Be warned `download` is not supported in IE (any version). – Scott Marcus Oct 17 '17 at 19:55
  • 2
    @Frame91 _"I'd like to listen to the download-event, mainly having a promise around save(), so that I can see whether or not the user clicked on `save` or `cancel`"_ , _"I cannot listen to save/cancel browser-events, can I?"_ No. There is not a "download-event" that am aware of. – guest271314 Oct 17 '17 at 19:59
  • 2
    @Frame91 See [Detect when user accepts to download a file](https://stackoverflow.com/q/41334881/) – guest271314 Oct 17 '17 at 20:06

0 Answers0