-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
56 lines (51 loc) · 1.85 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
document.addEventListener('DOMContentLoaded', () => {
const fileInput = document.getElementById('fileInput');
const uploadButton = document.getElementById('uploadBtn');
const fileList = document.getElementById('fileList');
const basePath = '/khdropbox';
uploadButton.addEventListener('click', () => {
const files = fileInput.files;
if (files.length > 0) {
const formData = new FormData();
for (let i = 0; i < files.length; i++) {
formData.append('files', files[i]);
}
fetch(`${basePath}/upload`, {
method: 'POST',
body: formData,
})
.then(response => response.text())
.then(message => {
console.log(message);
alert(message);
loadFiles();
})
.catch(error => {
console.error('Error uploading file:', error);
alert('Error uploading file.');
});
} else {
alert('Please select files.');
}
});
function loadFiles() {
fetch(`${basePath}/files`)
.then(response => response.json())
.then(files => {
fileList.innerHTML = '';
files.forEach(filename => {
const listItem = document.createElement('li');
const link = document.createElement('a');
link.href = `${basePath}/download/${filename}`;
link.textContent = filename;
listItem.appendChild(link);
fileList.appendChild(listItem);
});
})
.catch(error => {
console.error('Error loading files:', error);
// alert('Error loading files.');
});
}
loadFiles();
});