使用 Javascript 将多个文件下载为 zip 文件或文件夹中的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28891235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download multiple files as a zip or in a folder using Javascript?
提问by Rohan
Is it possible to download multiple files from an url into one created folder or as a zip?
是否可以将多个文件从一个 url 下载到一个创建的文件夹或作为 zip 文件?
I currently download a file given a url like so :
我目前下载一个文件,给定一个 url,如下所示:
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?
是否可以创建并指定哪个文件夹来下载所有文件或将所有下载的文件转换为 zip 文件?
回答by David No?l
In modern browsers, you could convert all the downloaded files into a zip with a combination 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.
在现代浏览器中,您可以将所有下载的文件转换为一个 zip,结合 JSZip(https://stuk.github.io/jszip/)和 AJAX blob 下载(使用 jQuery 的 ajax 方法将图像检索为 blob),尽管我希望您在处理大文件时会遇到性能问题。否则,无法使用浏览器 JS 在客户端上创建文件夹。