/** * @description downloadボタンを押下したらCSVファイルをzip圧縮してダウンロードする */ // ダウンロードボタン const downloadButton = document.getElementById('download-button'); const downloadButtonBase64 = document.getElementById('download-button-base64'); // ダウンロードボタンを押下した時の処理を記載する downloadButton.addEventListener('click', async () => { // URLを別のウィンドウで開く // GCSからZIPファイルのコンテンツが返る window.open('http://localhost:3000/downdload-gcs'); }); downloadButtonBase64.addEventListener('click', async () => { // ダウンロードボタンを無効化する downloadButtonBase64.disabled = true; try { // localhost:3000/downdload-gcs-json にリクエストを送る const response = await fetch('http://localhost:3000/downdload-gcs-json'); if (!response.ok ) throw new Error('Failed to fetch'); // JSONでdataにbase64が返ってくる const res = await response.json(); const base64 = res.data; // Base64をデコードしてZIPファイルを生成 const zipBlob = await fetch(`data:application/zip;base64,${base64}`).then(res => res.blob()); const a = document.createElement("a"); a.href = URL.createObjectURL(zipBlob); a.download = "serverFile.zip"; document.body.appendChild(a); a.click(); document.body.removeChild(a); } catch (e) { // エラーが発生した場合 console.error(e); alert('ダウンロードに失敗しました'); } finally { // ダウンロードボタンを有効化する downloadButtonBase64.disabled = false; } });