-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
98 lines (87 loc) · 3.72 KB
/
upload.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
document.getElementById('file').addEventListener('change', function() {
const fileInput = document.getElementById('file');
const uploadBtn = document.getElementById('uploadBtn');
if (fileInput.files.length > 0) {
document.getElementById('fileName').innerHTML = `<b>${fileInput.files[0].name}</b>`;
uploadBtn.disabled = false;
} else {
document.getElementById('fileName').innerHTML = '';
uploadBtn.disabled = true;
}
});
document.getElementById('uploadFileForm').addEventListener('submit', async function(event) {
event.preventDefault();
const { username, password } = authCredentials;
const fileInput = document.getElementById('file');
const statusMessage = document.getElementById('statusMessage');
const progressBar = document.getElementById('progressBar');
const progressRow = document.getElementById('progressRow');
if (fileInput.files.length > 0) {
const file = fileInput.files[0];
const formData = new FormData();
formData.append('file', file);
formData.append('username', username);
formData.append('password', password);
const fileDateTime = file.lastModified;
formData.append('fileDateTime', fileDateTime);
const xhr = new XMLHttpRequest();
xhr.open('POST', 'upload.php', true);
const uploadBtn = document.getElementById('uploadBtn');
const fileBtn = document.getElementById('file');
uploadBtn.disabled = true;
fileBtn.disabled = true;
const startTime = Date.now();
xhr.upload.onprogress = function(e) {
if (e.lengthComputable) {
const percentComplete = (e.loaded / e.total) * 100;
progressBar.style.width = percentComplete + '%';
progressBar.innerText = Math.round(percentComplete) + '%';
progressRow.style.display = 'block'; // Show the progress bar
}
};
xhr.onload = function() {
const endTime = Date.now();
const uploadTime = (endTime - startTime) / 1000;
const fileSize = fileInput.files[0].size;
const uploadRate = (fileSize / 1024 / uploadTime).toFixed(0);
if (xhr.status === 200) {
statusMessage.innerHTML = `File <b>${fileInput.files[0].name}</b> successfully uploaded. Upload time: <b>${uploadTime.toFixed(2)}</b> seconds. Upload rate: <b>${uploadRate}</b> KBps.`;
loadFileList();
} else {
statusMessage.innerHTML = 'Upload failed!';
}
progressBar.style.width = '0%';
progressBar.innerText = '';
progressRow.style.display = 'none'; // Hide the progress bar
document.getElementById('fileName').innerHTML = '';
uploadBtn.disabled = true;
fileBtn.disabled = false;
fileBtn.value = '';
};
statusMessage.innerHTML = `Uploading file ${fileInput.files[0].name}...`;
xhr.send(formData);
}
});
async function deleteFile(fileName) {
const { username, password } = authCredentials;
try {
const response = await fetch('getFileList.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password, delete: fileName }),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.json();
if (result.success) {
loadFileList();
} else {
console.error('Error deleting file:', result.error);
}
} catch (error) {
console.error('Error deleting file:', error);
}
}