Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use window.location.href to download multiple files?

I have the following javascript:

function downloadFiles(){
  var files = [];
  files.push('mysite.com/file1.txt');
  files.push('mysite.com/file2.txt');
  files.push('mysite.com/file3.txt');

  for(var ii=0; ii<files.length; ii++){
    window.location.href = files[ii];
  }
}

The problem is this only downloads the last file in the list because the first two files get overwritten by the last one. How can I wait for the user's input on each file before moving on to the next file?

like image 723
Grammin Avatar asked Dec 04 '22 09:12

Grammin


1 Answers

What I ended up doing:

function downloadFiles(){
  var files = [];
  files.push('file1.txt');
  files.push('file2.txt');
  files.push('file3.txt');

  for(var ii=0; ii<files.length; ii++){
    downloadURL(files[ii]);
  }
}

var count=0;
var downloadURL = function downloadURL(url){
  var hiddenIFrameID = 'hiddenDownloader' + count++;
  var iframe = document.createElement('iframe');
  iframe.id = hiddenIFrameID;
  iframe.style.display = 'none';
  document.body.appendChild(iframe);
  iframe.src = url;
}
like image 75
Grammin Avatar answered Dec 06 '22 22:12

Grammin