From fe43265c9d7d8bcb63acd3feb92991271a4cdb71 Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 14 Sep 2023 15:01:11 +0800 Subject: [PATCH] code refact --- src/Moonglade.Web/Pages/Admin/Category.cshtml | 25 +------------------ .../wwwroot/js/app/dataexport.js | 23 +++++++++++++++++ 2 files changed, 24 insertions(+), 24 deletions(-) create mode 100644 src/Moonglade.Web/wwwroot/js/app/dataexport.js diff --git a/src/Moonglade.Web/Pages/Admin/Category.cshtml b/src/Moonglade.Web/Pages/Admin/Category.cshtml index 4f0926cd3..8103924b6 100644 --- a/src/Moonglade.Web/Pages/Admin/Category.cshtml +++ b/src/Moonglade.Web/Pages/Admin/Category.cshtml @@ -5,6 +5,7 @@ } @section scripts{ + } diff --git a/src/Moonglade.Web/wwwroot/js/app/dataexport.js b/src/Moonglade.Web/wwwroot/js/app/dataexport.js new file mode 100644 index 000000000..2b11e9d0d --- /dev/null +++ b/src/Moonglade.Web/wwwroot/js/app/dataexport.js @@ -0,0 +1,23 @@ +function downloadCSV(csvData, filename) { + const blob = new Blob([csvData], { type: 'text/csv' }); + const url = URL.createObjectURL(blob); + const a = document.createElement('a'); + a.setAttribute('hidden', ''); + a.setAttribute('href', url); + a.setAttribute('download', filename); + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); +} + +function exportCSV(api, filename) { + fetch(api) + .then(response => response.json()) + .then(data => { + const csvData = convertJSONtoCSV(data); + downloadCSV(csvData, filename); + }) + .catch(error => { + console.error('Error:', error); + }); +} \ No newline at end of file