A bunch of random crap working towards the first (simplest) upload mechanism
This commit is contained in:
45
static/script.js
Normal file
45
static/script.js
Normal file
@@ -0,0 +1,45 @@
|
||||
function uploadFiles(submitButtonID, fileInputID, progressBarID, targetEndpoint, otherData) {
|
||||
const submitButton = document.getElementById(submitButtonID)
|
||||
submitButton.disabled = true
|
||||
|
||||
const files = document.getElementById(fileInputID).files
|
||||
|
||||
if (!files[0]) {
|
||||
alert('Please select at least one file')
|
||||
submitButton.disabled = false
|
||||
return
|
||||
}
|
||||
|
||||
const formData = new FormData()
|
||||
|
||||
for(var i = 0; i < files.length; i++) {
|
||||
formData.append("files[]", files[i])
|
||||
}
|
||||
|
||||
for(const key in otherData) {
|
||||
formData.append(key, otherData[key])
|
||||
}
|
||||
|
||||
var xhr = new XMLHttpRequest()
|
||||
xhr.open("POST", targetEndpoint)
|
||||
xhr.onload = function() {
|
||||
if (xhr.status === 200) {
|
||||
window.location.href = "/"
|
||||
} else {
|
||||
alert("Something went wrong during the upload, please try again later")
|
||||
submitButton.disabled = false
|
||||
}
|
||||
}
|
||||
xhr.upload.onprogress = function(event) {
|
||||
if(event.lengthComputable) {
|
||||
const percentComplete = (event.loaded / event.total) * 100
|
||||
document.getElementById(progressBarID).value = percentComplete
|
||||
}
|
||||
}
|
||||
xhr.onerror = function() {
|
||||
alert("An error occurred, please try again later")
|
||||
submitButton.disabled = false
|
||||
}
|
||||
|
||||
xhr.send(formData)
|
||||
}
|
||||
Reference in New Issue
Block a user