0

I have situation where I need to simulate several download requests on one button click. My code is here:

download(filename: string) {
    console.log(filename);
    const href = <my href>;
    let a = document.createElement('a');
    a.setAttribute('style', 'display: none');
    a.href = href;
    a.download = filename;
    a.click();
    a.remove();
  }

  downloadFiles(className) {
    for (let docLink of this.component.nativeElement.getElementsByClassName(className)) {
      docLink.click();
    }
  }

but the problem here is that file download is initiated only for one file instead of 3. What might be the problem?

Alex
  • 1,940
  • 2
  • 18
  • 36
  • What does this watch return? this.component.nativeElement.getElementsByClassName(className).length ? – yonexbat Apr 10 '19 at 20:54
  • it returns 3 elements – Alex Apr 10 '19 at 20:56
  • Try setting a `setTimeout` to delay the clicks. https://stackoverflow.com/questions/18451856/how-can-i-let-a-user-download-multiple-files-when-a-button-is-clicked – bobjoe Apr 10 '19 at 21:02
  • 1
    The problem is that once you click on the first download, the dialog opens for the user to pick where to save the file. While they're doing that, the other clicks are registering, but since there's already an open dialog, they are ignored. – Heretic Monkey Apr 10 '19 at 21:17
  • Possible duplicate of [How can I let a user download multiple files when a button is clicked?](https://stackoverflow.com/questions/18451856/how-can-i-let-a-user-download-multiple-files-when-a-button-is-clicked) – Heretic Monkey Apr 10 '19 at 21:18

0 Answers0