Skip to content

Instantly share code, notes, and snippets.

@rodydavis
Last active August 9, 2021 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodydavis/bc800d863dcf8c76cb68efe10bd3233b to your computer and use it in GitHub Desktop.
Save rodydavis/bc800d863dcf8c76cb68efe10bd3233b to your computer and use it in GitHub Desktop.
/**
* 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