简体   繁体   中英

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

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?

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.

<!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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM