最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Download multiple files as a zip or in a folder using Javascript? - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 1

In 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>
发布评论

评论列表(0)

  1. 暂无评论