Download multiple files as a zip or in a folder using Javascript?

14,310

Solution 1

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.

Solution 2

<!DOCTYPE html>
<html>
    <head><script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.5.0/jszip.min.js" type="text/javascript"> </script>

<script src="https://cdnjs.cloudflare.com/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.com/u/3269942?s=52&v=4',
                'https://avatars2.githubusercontent.com/u/1234650?s=88&u=bcb313dffc1180bf90c818879749c5184e922aa2&v=4',
                'https://avatars0.githubusercontent.com/u/28157230?s=88&u=e9d1130e4fa2760fa2fccf5f1d2f1c03708b0e4d&v=4'
            ];

            var nombre = "Zip_img";
            //The function is called
            compressed_img(urls, nombre);
            function compressed_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>
Share:
14,310
Rohan
Author by

Rohan

Dropping a cloud rap mixtape soon. e m o t i o n a l

Updated on June 14, 2022

Comments

  • Rohan
    Rohan almost 2 years

    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?