Last active
August 9, 2021 21:42
-
-
Save rodydavis/bc800d863dcf8c76cb68efe10bd3233b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Download a list of files. | |
* @author speedplane | |
* @link https://github.com/robertdiers/js-multi-file-download/blob/master/src/main/resources/static/multidownload.js | |
*/ | |
function download_files(files) { | |
function download_next(i) { | |
if (i >= files.length) { | |
return; | |
} | |
var a = document.createElement('a'); | |
a.href = files[i].download; | |
a.target = '_parent'; | |
// Use a.download if available, it prevents plugins from opening. | |
if ('download' in a) { | |
a.download = files[i].filename; | |
} | |
// Add a to the doc for click to work. | |
(document.body || document.documentElement).appendChild(a); | |
if (a.click) { | |
a.click(); // The click method is supported by most browsers. | |
} else { | |
$(a).click(); // Backup using jquery | |
} | |
// Delete the temporary link. | |
a.parentNode.removeChild(a); | |
// Download the next file with a small timeout. The timeout is necessary | |
// for IE, which will otherwise only download the first file. | |
setTimeout(function() { | |
download_next(i + 1); | |
}, 500); | |
} | |
// Initiate the first download. | |
download_next(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment