Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...