Download multiple files using js

The browser only pops up a download window, but you can download multiple files. After one file is downloaded, you can continue to download another file.

$(document).on("click", "#standLoad", function(){
            
            let triggerDelay = 100;
            let removeDelay = 1000;
            //Store multiple download urls,
            let url_arr=[];
            $('.standardload ').each(function(){
                   url_arr.push($(this).attr('href'));
             })
            
            url_arr.forEach(function(item,index){
                _createIFrame(item, index * triggerDelay, removeDelay);
            })
            function _createIFrame(url, triggerDelay, removeDelay) {
                //Add iframe dynamically, set src, then delete
                setTimeout(function() {
                    var frame = $('<iframe style="display: none;" class="multi-download"></iframe>');
                    frame.attr('src', url);
                    $(document.body).after(frame);
                    setTimeout(function() {
                        frame.remove();
                    }, removeDelay);
                }, triggerDelay);
            }
            

        });


Reference: https://segmentfault.com/q/1010000012022240.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325920169&siteId=291194637