add download xlsx ready file

This commit is contained in:
Ahmed 2024-09-12 15:05:10 +03:00
parent a1e0399a04
commit 6e147aede5
1 changed files with 75 additions and 1 deletions

View File

@ -171,4 +171,78 @@ function loadCityDetails() {
} }
} }
} }
$(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);
// }
// });