複数ファイルをDLさせたくなったんだけど
なかなかうまくいかなくて、、、
そのときの対応内容をメモメモ
1ファイルのDLが終わったら、
次のファイルのDLが始まる感じ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
$('#dl').on('click', function(e){ let urlList = [url1, url2, url3, ... ]; fileDl(urlList, 0); }); function fileDl(urlList, idx) { let href = urlList[idx]; console.log(href); loadFile(href, function (responce) { if (responce!=null){ downloadAsFile(responce); }else{ alert("DLに失敗しました"); } if (idx >= urlList.length-1) { $('#loading').remove(); } else { fileDl(urlList, idx+1) } }); } var loadFile = function(href, cb) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(){ if (this.readyState == 4 ){ if ( this.status == 200) { cb(this.response); }else{ cb(null); } } } xhr.open('GET', href, true); xhr.responseType = 'blob'; xhr.send(null); } var downloadAsFile = function(blob) { var objectURL = window.URL.createObjectURL(blob); var link = document.createElement("a"); document.body.appendChild(link); link.href = objectURL; link.download = 'filename'; link.click(); document.body.removeChild(link); }; |