admin管理员组文章数量:1320800
Is it possible to download multiple files from an url into one created folder or as a zip?
I currently download a file given a url like so:
var download_url = "";
window.location.href = download_url;
If I were to have an array of urls, I could do something like...
for each url in urls
window.location.href = url;
This works, but will lead to individual downloaded files in the downloads folder. This can be messy.
Is it possible to create and specify which folder to download all of the files or convert all the downloaded files into a zip?
Is it possible to download multiple files from an url into one created folder or as a zip?
I currently download a file given a url like so:
var download_url = "";
window.location.href = download_url;
If I were to have an array of urls, I could do something like...
for each url in urls
window.location.href = url;
This works, but will lead to individual downloaded files in the downloads folder. This can be messy.
Is it possible to create and specify which folder to download all of the files or convert all the downloaded files into a zip?
Share Improve this question edited Jan 24, 2021 at 8:05 Martin Brisiak 4,06112 gold badges39 silver badges51 bronze badges asked Mar 6, 2015 at 2:06 RohanRohan 1,3523 gold badges18 silver badges45 bronze badges 2- possible duplicate of Downloading multiple files and zip - Chrome extension – Wesley Smith Commented Mar 6, 2015 at 2:09
- @DelightedD0D the question you linked is regarding Chrome Extensions and the top rated answer would only work with Chrome – Rohan Commented Mar 6, 2015 at 2:30
2 Answers
Reset to default 1In modern browsers, you could convert all the downloaded files into a zip with a bination of JSZip (https://stuk.github.io/jszip/) and AJAX blob downloads (Using jQuery's ajax method to retrieve images as a blob), although I expect you will run into performance issues when dealing with large files. Otherwise, it isn't possible to create a folder on the client with browser JS.
<!DOCTYPE html>
<html>
<head><script src="https://cdnjs.cloudflare./ajax/libs/FileSaver.js/1.3.8/FileSaver.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/jszip/3.5.0/jszip.min.js" type="text/javascript"> </script>
<script src="https://cdnjs.cloudflare./ajax/libs/jszip-utils/0.1.0/jszip-utils.js" type="text/javascript"> </script>
</head>
<body>
</body>
<script>
var zip = new JSZip();
var count = 0;
var zipFilename = "zipFilename.zip";
var urls = [
'https://avatars2.githubusercontent./u/3269942?s=52&v=4',
'https://avatars2.githubusercontent./u/1234650?s=88&u=bcb313dffc1180bf90c818879749c5184e922aa2&v=4',
'https://avatars0.githubusercontent./u/28157230?s=88&u=e9d1130e4fa2760fa2fccf5f1d2f1c03708b0e4d&v=4'
];
var nombre = "Zip_img";
//The function is called
pressed_img(urls, nombre);
function pressed_img(urls, nombre) {
var zip = new JSZip();
var count = 0;
var name = nombre + ".zip";
urls.forEach(function(url) {
JSZipUtils.getBinaryContent(url, function(err, data) {
if (err) {
throw err;
}
zip.file(url, data, {
binary: true
});
count++;
if (count == urls.length) {
zip.generateAsync({
type: 'blob'
}).then(function(content) {
saveAs(content, name);
});
}
});
});
}
</script>
</html>
本文标签: jqueryDownload multiple files as a zip or in a folder using JavascriptStack Overflow
版权声明:本文标题:jquery - Download multiple files as a zip or in a folder using Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742081117a2419704.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论