Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

jquery - Multiple file download using javascript not working

I am using the javascript to download multiple file from url.

I have used the following url to do this but not find any solutions,

Its working fine for firefox and google chrome but not work with ie and edge

I have used the following code.

reportFileList.forEach((report, index) => {
    var downloadUrl = report
    setTimeout(function() {
        var a = document.createElement('a');
        a.href = downloadUrl;
        a.target = '_parent';
        if ('download' in a) {
            a.download = downloadUrl;
        }

        (document.body || document.documentElement).appendChild(a);
        if (a.click) {
            a.click(); // The click method is supported by most browsers.
        } 
        a.parentNode.removeChild(a);
    }, 500);
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That piece of code works (tested in Chrome) the problem must be somewhere else:

  • Maybe the reportFileList var is malformed.
  • Some browsers prompt you to make multiple downloads, it must be enabled.

Example: http://js.do/code/161479

<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<p style="line-height: 18px; font-size: 18px;  font-family: times;">
Click "<i>Load samples</i>" to view and edit more JS samples.<br>

<script>
var reportFileList = ['https://www.example.com','https://www.example.com','https://www.example.com'];
reportFileList.forEach((report, index) => {
            var downloadUrl = report
                setTimeout(function() {
                    var a = document.createElement('a');
                    a.href = downloadUrl;
                    a.target = '_parent';
                    if ('download' in a) {
                        a.download = downloadUrl;
                    }

                    (document.body || document.documentElement).appendChild(a);
                    if (a.click) {
                        a.click(); // The click method is supported by most browsers.
                    } 
                    a.parentNode.removeChild(a);
                }, 500);



    });
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...