0

I have a page which has a many links of files to be downloaded individually. I thought it would be a good to have feature to trigger download for all the files with a single click. Here's the script which I wrote to test this-

$('tbody tr a').slice(1).each(function(){  //don't mind the slice().
    console.log('starting download of: ' + $(this).attr('href')); // for debugging.
    $(this).attr('target','_blank'); //  so that a new page is opened for download.
    window.location.href = $(this).attr('href');
})

The problem is that the script only triggers the download of only first download link. However, if I see console, the log is printed for all the files. I think it's happening because of page redirection. Can anyone help me get around this?

Abhishek
  • 681
  • 1
  • 6
  • 25

2 Answers2

2

Try substituting download attribute for window.location.href = $(this).attr('href'), call .click() on each <a> element within filtered selector $("tbody tr a").slice(1) at .each() at click of <button> element

var a = $("tbody tr a").slice(1);

$("button").click(function(e) {
  // download `a.txt`, `b.txt`
  a.each(function() {
    this.click()
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr>
      <td>
        <a href="data:text/plain,1" download="1.txt">1</a>
      </td>
    </tr>
    <tr>
      <td>
        <a href="data:text/plain,a" download="a.txt">a</a>
      </td>
    </tr>
    <tr>
      <td>
        <a href="data:text/plain,b" download="b.txt">b</a>
      </td>
    </tr>
  </tbody>
  <!-- click to download `a.txt`, `b.txt` -->
  <button>download all files</button>
guest271314
  • 1
  • 15
  • 104
  • 177
  • This page doesn't have a `download` attribute defined on links. So I added it by iterating and doing `$(this).attr('target','_blank').attr('download',filename);`. But even after this, `.click()` doesn't start the file download. – Abhishek Jul 16 '16 at 07:26
  • Have you tried `js` at stacksnippets? Did you call `.click()` chained to `$(this).attr('target','_blank').attr('download',filename)`? Try calling `this.click()` on `DOM` element instead of jQuery object. – guest271314 Jul 16 '16 at 07:28
  • Thanks! the second one, i.e., `this.click()` works. What does this mean though? – Abhishek Jul 16 '16 at 07:44
  • Wait another thing, this only triggered 6 downloads. What about rest of them? – Abhishek Jul 16 '16 at 07:45
  • _"Wait another thing, this only triggered 6 downloads. What about rest of them?"_ What do you mean? – guest271314 Jul 16 '16 at 07:47
  • Got this. There's a setting which limits the maximum number of simultaneous connections to a single server. For my case this is 6 (probably for windows OS). – Abhishek Jul 16 '16 at 07:50
  • See https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click , http://stackoverflow.com/questions/773639/how-can-i-simulate-an-anchor-click-via-jquery – guest271314 Jul 16 '16 at 08:00
0

You may want to see this answer here:

jquery trigger ctrl + click

Just run on all of your links and trigger ctrl+click in order to open links in new page.

Community
  • 1
  • 1
Kosta B.
  • 271
  • 2
  • 12