2017-07-17 16 views
0

私はurlから複数のファイルをダウンロードするためにjavascriptを使用しています。私はこれを行うが、任意の解決策を見つけることができませんするには、次のURLを使用しているjavascriptを使用しない複数のファイルのダウンロード

、FirefoxとGoogle Chromeの細かい作業が、私は、次のコードを使用している

すなわちエッジでは動作しませ

その。コードの一部は、(Chromeでテストした)動作することを

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

ブラウザ**の開発者**ツールコンソールでエラーが発生しましたか? –

+0

いいえ、最後のファイル –

+0

をダウンロードしてもエラーは発生しません。firefoxとgoogle chromeはうまく動作しますが、ieとエッジでは機能しません。 –

答えて

0

私は、コードに従うことによってそれを解決した - > は他のヘルプ誰おそれ。

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(); 
0

問題は、どこか別の場所でなければなりません:

  • たぶんreportFileListのvarが不正な形式です。
  • ブラウザによっては、複数のダウンロードを促すメッセージが表示されることがあります。

例: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> 
+0

はい、クロームでうまく動作していますが、私の質問をはっきりとチェックしていますか? –

+0

あなたの質問をはっきりと読んだことがありますが、後ほどその質問を編集して問題を編集したことが問題です:https://stackoverflow.com/posts/45143177/revisions –

関連する問題