diff options
author | Joris | 2024-05-20 09:40:11 +0200 |
---|---|---|
committer | Joris | 2024-05-20 09:40:11 +0200 |
commit | 436ddf6f23242eb709b591cd5e9cbf1553f8d390 (patch) | |
tree | dfed58b5e553f131fd3009f03f095ca40efc5949 /public/main.js | |
parent | 6baa0419d3b5eb63c70be446226a321f900e433d (diff) |
Allow to upload file and download from given link
Diffstat (limited to 'public/main.js')
-rw-r--r-- | public/main.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/public/main.js b/public/main.js new file mode 100644 index 0000000..1729d38 --- /dev/null +++ b/public/main.js @@ -0,0 +1,48 @@ +window.onload = function() { + const form = document.querySelector('form') + + if (form !== null) { + const submit = document.querySelector('input[type="submit"]') + const loading = document.querySelector('.g-Loading') + const error = document.querySelector('.g-Error') + + function showError(msg) { + loading.style.display = 'none' + submit.disabled = false + error.innerText = msg + error.style.display = 'block' + } + + form.onsubmit = function(event) { + event.preventDefault() + + loading.style.display = 'flex' + submit.disabled = true + error.style.display = 'none' + + const key = document.querySelector('input[name="key"]').value + const expiration = document.querySelector('select[name="expiration"]').value + const file = document.querySelector('input[name="file"]').files[0] + + // Wait a bit to prevent showing the loader too briefly + setTimeout(function() { + const xhr = new XMLHttpRequest() + xhr.open('POST', '/', true) + xhr.onload = function () { + if (xhr.status === 200) { + window.location = `/${xhr.responseText}` + } else { + showError(`Error uploading: ${xhr.status}`) + } + } + xhr.onerror = function () { + showError('Upload error') + } + xhr.setRequestHeader('X-FileName', file.name) + xhr.setRequestHeader('X-Expiration', expiration) + xhr.setRequestHeader('X-Key', key) + xhr.send(file) + }, 500) + } + } +} |