transfersh/scripts/upload.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-05-08 22:46:12 +02:00
// Enable JS-only functionalities and disable nojs ones.
Array.prototype.slice.call(document.getElementsByClassName("jsonly")).forEach(e => e.removeAttribute("style"));
Array.prototype.slice.call(document.getElementsByClassName("nojs")).forEach(e => e.style.display = "none");
document.getElementById('fileinput').setAttribute('multiple','');
// get form
const form = document.getElementById('dropzone');
function upload(file,exp,maxdl) {
2021-05-08 22:46:12 +02:00
// create the XHR
const xhr = new XMLHttpRequest();
// Create list item
const list=document.getElementById('links');
var entry=document.createElement('li');
entry.innerHTML = file.name + '<br /><progress></progress>';
list.appendChild(entry);
// Progress bar
xhr.upload.addEventListener('progress', function(e) {
entry.innerHTML = file.name + '<br /><progress max="' + e.total + '" value="' + e.loaded +'"></progress>';
});
// Links response
xhr.onload = () => {
entry.innerHTML = '<a target="_blank" href="' + xhr.responseText + '">' + xhr.responseText + '</a>';
2021-05-08 22:46:12 +02:00
};
// create and send the request
xhr.open('PUT', '/' + file.name);
// add headers
if (exp != "") {
xhr.setRequestHeader('Max-Days',exp);
}
2021-06-06 15:00:36 +02:00
if (maxdl != 0) {
xhr.setRequestHeader('Max-Downloads',maxdl);
}
// send file
2021-05-08 22:46:12 +02:00
xhr.send(file);
}
// add event listener on form
form.addEventListener('submit', e => {
// disable normal sending
e.preventDefault();
// get file list
const files = document.getElementById('fileinput').files;
const exp = document.getElementById('expiration').value;
const maxdl = document.getElementById('maxdl').value;
2021-05-08 22:46:12 +02:00
// upload each file
for (let i = 0; i < files.length; i++) {
upload(files[i],exp,maxdl);
2021-05-08 22:46:12 +02:00
}
// reset file list
document.getElementById('fileinput').value='';
2021-05-08 22:46:12 +02:00
});