diff --git a/static/js/citydetails.js b/static/js/citydetails.js index dba2ec8..a8d2b06 100644 --- a/static/js/citydetails.js +++ b/static/js/citydetails.js @@ -171,4 +171,78 @@ function loadCityDetails() { } } -} \ No newline at end of file +} + + + + +$(document).ready( function() { + document.getElementById('downloadButton').addEventListener('click', async () => { + // const filename = 'testfilename'; + const username = document.getElementById("userName").innerText; + + + const filename = getCityDetails()+' '+ username +'.xlsx'; +console.log(filename); + + // const filename = 'example.xlsx'; // Specify the filename you want + const url = `http://192.168.1.6/download?filename=${encodeURIComponent(filename)}`; + + try { + const response = await fetch(url); + + if (!response.ok) { + throw new Error('Network response was not ok'); + } + + // Create a blob from the response + const blob = await response.blob(); + + // Create a link element + const a = document.createElement('a'); + a.href = URL.createObjectURL(blob); + a.download = filename; // Set the filename for download + document.body.appendChild(a); + a.click(); + + // Clean up + document.body.removeChild(a); + } catch (error) { + console.error('Error downloading the file:', error); + } +}); + + +}); + + +// document.getElementById('downloadButtson').addEventListener('click', async () => { +// const filename = getCityDetails(); +// console.log(filename); + +// // const filename = 'example.xlsx'; // Specify the filename you want +// const url = `http://${serverEnv.ip}/download?filename=${encodeURIComponent(filename)}`; + +// try { +// const response = await fetch(url); + +// if (!response.ok) { +// throw new Error('Network response was not ok'); +// } + +// // Create a blob from the response +// const blob = await response.blob(); + +// // Create a link element +// const a = document.createElement('a'); +// a.href = URL.createObjectURL(blob); +// a.download = filename; // Set the filename for download +// document.body.appendChild(a); +// a.click(); + +// // Clean up +// document.body.removeChild(a); +// } catch (error) { +// console.error('Error downloading the file:', error); +// } +// }); \ No newline at end of file