How can I use window.location.href to upload 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 that it only downloads the last file in the list, because the first two files are overwritten by the last. How can I wait for user input in each file before moving on to the next file?

+4
source share
2 answers

What I finished:

 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; } 
+7
source

If you change your code to use window.open() instead of window.location , you can run all three downloads at once.

I know that this does not satisfy the requirement of waiting for user input before presenting each of the downloads, but it does not correspond to the spirit of your source code. Hope this helps a bit.

 function downloadFiles(){ var files = []; files.push('file1.txt'); files.push('file2.txt'); files.push('file3.txt'); for(var ii=0; ii<files.length; ii++){ window.open(files[ii]); } } 
+6
source

Source: https://habr.com/ru/post/1499036/


All Articles