/** * @description downloadボタンを押下したらCSVファイルをzip圧縮してダウンロードする */ // ダウンロードボタン const downloadButtonServer = document.getElementById('download-button-server'); // ダウンロードボタンを押下した時の処理を記載する downloadButtonServer.addEventListener('click', async () => { // Blobで取得する方法 console.log('downloadButtonServer'); // ダウンロードボタンを無効化する downloadButtonServer.disabled = true; // ダウンロード処理を実行する try { // localhost:3000/downdload にリクエストを送る const response = await fetch('http://localhost:3000/downdload'); if (!response.ok) throw new Error('Failed to fetch'); // ZIPファイルを取得 const zipBlob = await response.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 { // ダウンロードボタンを有効化する downloadButtonServer.disabled = false; } }); // ダウンロードボタン(アップロード) const downloadButtonUpload = document.getElementById('download-button-upload'); // ダウンロードボタンを押下した時の処理を記載する downloadButtonUpload.addEventListener('click', async () => { console.log('downloadButtonUpload'); // ダウンロードボタンを無効化する downloadButtonUpload.disabled = true; // サーバーにアップロード処理APIを送信する try { // localhost:3000/generate-zip にリクエストを送る const response = await fetch('http://localhost:3000/generate-zip'); if (!response.ok) throw new Error('Failed to fetch'); // レスポンスからURLを取得 const { url } = await response.json(); // 取得したURLを開く window.open(url); } catch (e) { // エラーが発生した場合 console.error(e); alert('ダウンロードに失敗しました'); } finally { // ダウンロードボタンを有効化する downloadButtonUpload.disabled = false; } });