0

I'm trying to make download function (only where checkbox checked can be downloaded and download function is already done).

when I check only one, It works fine. download well

but I want to check many and download too.

I set download_file() works only one at a time in need

download_file() is onClick event and execute by this way

d_tr.querySelector('.test > a').click();

what I want is execute another function after first one finished.

It doesn't matter Javascript or Jquery.

js $(document).ready(function () { $(".compress").click(function () {

    var size = document.getElementsByName("check").length;
    for(var i = 0; i < size; i++){
        if(document.getElementsByName("check")[i].checked == true){
            var d_val=document.getElementsByName("check")[i].value;;
            var d_td = document.getElementById(d_val);
            var d_tr = d_td.parentElement;
            d_tr.querySelector('.test > a').click();
            $.when(d_tr.querySelector('.test > a').click()).done(function () {
               console.log("finished");
            });

        }
    }
    });
});

var oneClick = true;
function download_file(event, filename) {
    if (oneClick) {
        oneClick = false;

        req = request({
            method: 'GET',
            uri   : fileURL,
        });
        var out = fs.createWriteStream(finalPath);
        req.pipe(out); 
        req.on('response', function (data) {
            console.log();
        });

        req.on('data', function (chunk) {
             console.log();
        });

        req.on('end', function () {
            oneClick = true;
            document.getElementById(subW).onclick = null;
        });
    }
    else {
        alert('somethins is downloading');
    }
}

html

<tr>
  <td id="10Mb.dat"><input type="checkbox" name='check' value='10Mb.dat'/>File10MB
  </td>
  <td class="test"><a class="checkBtn checkBtn1" id="checkBtn1" onclick="download_file(event, '10Mb.dat')">download</a></td>
</tr>
<tr>
  <td id="100mb.bin"><input type="checkbox" name='check' value='100mb.bin' />File100MB
  </td>
  <td class="test"><a class="checkBtn checkBtn2" id="checkBtn2" onclick="download_file(event, '100mb.bin')">download</a></td>
</tr>
 <button class="btn btn-primary compress">download start</button>
g.developer
  • 155
  • 1
  • 3
  • 12
  • Can you utilize Javascript callbacks? https://www.w3schools.com/jquery/jquery_callback.asp – InspiredBy Feb 25 '19 at 07:55
  • _"what I want is execute another function after first one finished."_ Are you trying to determine if a user accepted an offer to download a file, and if so, if and when that download completed? – guest271314 Feb 25 '19 at 08:08
  • May be you can change the code with a little technique. After clicking on compress button, the click callback function will collect all selected checkbox and store the filename on an global array, then call a single download function where request file from array list with a global index, after request end you can increase index and call again the download function recursively. – Ashraful Karim Miraz Feb 25 '19 at 08:19
  • @guest271314 when user click download button, it has to download checked files in order – g.developer Feb 25 '19 at 08:20
  • 1
    @g.developer How to you contend to determine if a user accepted an offer to download a file? And if a user did accept an offer to download a file how will you determine if or when that download completes? You can offer a single `.zip` file for download containing one or more files. – guest271314 Feb 25 '19 at 08:21
  • @g.developer There is a prospective solution https://stackoverflow.com/a/41336449/ to determine if a user downloaded at file at the linked question that requires further user action besides only downloading the file, and there is no guarantee that the user will perform that action, or regain focus at that tab. See [Multiple download links to one zip file before download javascript](https://stackoverflow.com/q/37176397/) for a solution to include multiple files or directories in a single `.zip` file – guest271314 Feb 25 '19 at 08:28
  • @guest271314 I already did making downloand files in a single zip file, but need to change to download files in order.. I'll check your suggestion – g.developer Feb 25 '19 at 08:48

0 Answers0