使用javascript的多个文件下载不起作用 [英] Multiple file download using javascript not working

查看:119
本文介绍了使用javascript的多个文件下载不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javascript从url下载多个文件.

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,

它对于Firefox和谷歌浏览器都可以正常工作,但不适用于ie和edge

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

我使用了以下代码.

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);
});

推荐答案

我已通过以下代码解决了它-> 也许对某人有帮助.

I have solved it with following code--> Maybe it helps somebody.

function download_files(files) {
function download_next(i) {
 if (i >= files.length) {
   return;
 }
 var a = document.createElement('a');
 a.href = files[i].download;
 a.target = '_blank';

 if ('download' in a) {
   a.download = files[i].download;
 }

 (document.body || document.documentElement).appendChild(a);
 if (a.click) {
   a.click(); // The click method is supported by most browsers.
 }
 else {
    window.open(files[i].download);
 }
 console.log('1');
 a.parentNode.removeChild(a);
 setTimeout(function() {
   download_next(i + 1);
 }, 5000);
}
// Initiate the first download.
download_next(0);
}

function do_dl() {
 download_files([
   { download: "https://www.example.com"},
   { download: "https://www.example.com"},
   { download: "https://www.example.com"},
   { download: "https://www.example.com"},
 ]);
};


do_dl();

这篇关于使用javascript的多个文件下载不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆