From 1e776b35bed788ad0c9332ac67b86b2ce4b9b826 Mon Sep 17 00:00:00 2001 From: Matt Strapp Date: Tue, 22 Feb 2022 18:13:53 -0600 Subject: Maybe add CSV support Signed-off-by: Matt Strapp --- src/public/css/style.css | 4 ++-- src/public/js/form.js | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) (limited to 'src/public') diff --git a/src/public/css/style.css b/src/public/css/style.css index b1651df..a106926 100644 --- a/src/public/css/style.css +++ b/src/public/css/style.css @@ -28,7 +28,7 @@ p { /* Used for id=button1 */ -#actuate_but { +#actuate-but { background-color: #0a3f73; border: solid; border-color: #d5d6d2; @@ -45,7 +45,7 @@ p { /* Used for id=button1 - What the button looks like when a user puts one's cursor on it */ -#actuate_but:hover { +#actuate-but:hover { background-color: white; color: maroon; cursor: pointer; diff --git a/src/public/js/form.js b/src/public/js/form.js index 581d5a3..3fbeb50 100644 --- a/src/public/js/form.js +++ b/src/public/js/form.js @@ -1,3 +1,5 @@ +// This is split into comments so it doesn't look as gross with the closure tabs + window.onload = function () { document.getElementById('nojs').hidden = true; document.getElementById('block').hidden = false; @@ -10,6 +12,7 @@ document.getElementById('upload').onsubmit = function () { document.getElementById('upload-err').innerText = ''; document.getElementById('actuate-err').innerText = ''; document.getElementById('upload-response').innerText = ''; + document.getElementById('download-link').innerText = ''; // Make AJAX request let xhr = new XMLHttpRequest(); xhr.open('POST', '/api/v1/upload'); @@ -43,8 +46,7 @@ function actuate(file) { let xhr = new XMLHttpRequest(); xhr.open('POST', '/api/v1/actuate'); let data = { - name: file.name, - path: file.path, + file: file.file }; xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8'); xhr.setRequestHeader('X-CSRF-TOKEN', file.csrf); @@ -54,6 +56,7 @@ function actuate(file) { let response = JSON.parse(xhr.responseText); if (xhr.status === 200) { console.log(response); + createDownload(response.file); } else { // Display upload error message to the user document.getElementById('actuate-err').innerText = response.error; @@ -61,6 +64,20 @@ function actuate(file) { if (xhr.status === 500) console.error(response.error_msg); } + return; } }; +} + + +// Creates the download element +function createDownload(response) { + const tempName = response.filename; + const downloadName = response.name.split('.')[ 0 ]; + const downloadLink = document.createElement('a'); + downloadLink.setAttribute('href', `/api/v1/download/?filename=${tempName}`); + downloadLink.setAttribute('download', `${downloadName}.csv`); + downloadLink.innerText = 'Download CSV of results here.'; + document.getElementById('download-link').appendChild(downloadLink); + return; } \ No newline at end of file -- cgit v1.2.3