我正在使用javascript从网址下载多个文件。
我使用以下网址执行此操作但未找到任何解决方案,
它适用于Firefox和谷歌浏览器但不适用于ie和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);
});
答案 0 :(得分:0)
这段代码可以运行(在Chrome中测试)问题必须在其他地方:
<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>
答案 1 :(得分:0)
我用以下代码解决了这个问题 - &gt; 也许它对某人有帮助。
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();
答案 2 :(得分:0)
我有同样的问题。对我有用的修复方法是将a
代码的目标更改为_balnk
,并在浏览器中允许弹出窗口权限。