-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
192 lines (168 loc) · 5.91 KB
/
index.html
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!DOCTYPE html>
<html>
<head>
<title>File Upload with Progress and Speed</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
flex-direction: column;
}
#upload-form {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
text-align: center;
max-width: 400px;
width: 90%;
}
h2 {
margin-top: 0;
}
input[type="file"] {
margin-bottom: 10px;
width: 100%;
box-sizing: border-box;
}
input[type="button"] {
background-color: #4caf50;
color: #fff;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.2s ease;
width: 100%;
box-sizing: border-box;
}
input[type="button"]:hover {
background-color: #45a049;
transform: scale(1.05);
}
#progress-bar {
width: 100%;
height: 20px;
margin-top: 10px;
display: none;
border-radius: 10px;
overflow: hidden;
background-color: #ddd;
}
#progress-bar::-webkit-progress-bar {
background-color: #ddd;
}
#progress-bar::-webkit-progress-value {
background-color: #4caf50;
transition: width 0.3s ease-in-out;
}
#progress-bar::-moz-progress-bar {
background-color: #4caf50;
transition: width 0.3s ease-in-out;
}
#status {
margin-top: 10px;
font-size: 14px;
color: #555;
min-height: 40px;
line-height: 20px;
text-align: left;
display: flex;
align-items: center;
justify-content: center;
background: #f4f4f4;
padding: 5px;
border: 1px solid #ddd;
border-radius: 5px;
width: 100%;
box-sizing: border-box;
overflow: hidden;
word-wrap: break-word;
}
/* Responsive Design */
@media (max-width: 768px) {
#upload-form {
padding: 15px;
}
input[type="file"], input[type="button"] {
font-size: 14px;
padding: 10px;
}
#status {
font-size: 12px;
}
}
</style>
</head>
<body>
<div id="upload-form">
<h2>Upload Multiple Files</h2>
<form id="fileUploadForm" enctype="multipart/form-data">
<input type="file" id="fileInput" name="files[]" multiple>
<br>
<input type="button" value="Upload" id="uploadButton">
<progress id="progress-bar" value="0" max="100"></progress>
<div id="status">Waiting for upload...</div>
</form>
</div>
<script>
document.getElementById('uploadButton').addEventListener('click', function() {
const form = document.getElementById('fileUploadForm');
const files = document.getElementById('fileInput').files;
const progressBar = document.getElementById('progress-bar');
const status = document.getElementById('status');
if (files.length === 0) {
alert('Please select at least one file.');
return;
}
const formData = new FormData();
for (let i = 0; i < files.length; i++) {
formData.append('files[]', files[i]);
}
const xhr = new XMLHttpRequest();
xhr.open('POST', '/upload', true);
// Show the progress bar and reset status
progressBar.style.display = 'block';
status.textContent = 'Starting upload...';
let startTime = Date.now();
// Update progress bar and calculate transfer speed
xhr.upload.onprogress = function(event) {
if (event.lengthComputable) {
const percentComplete = (event.loaded / event.total) * 100;
progressBar.value = percentComplete;
const elapsedTime = (Date.now() - startTime) / 1000; // Time in seconds
const speed = (event.loaded / elapsedTime) / (1024 * 1024); // Speed in MB/s
status.textContent = `Transferred: ${Math.round(event.loaded / (1024 * 1024))} MB /
${Math.round(event.total / (1024 * 1024))} MB
(${percentComplete.toFixed(2)}%) at ${speed.toFixed(2)} MB/s`;
}
};
// Reset progress bar and display success message upon completion
xhr.onload = function() {
if (xhr.status === 200) {
alert('Files uploaded successfully!');
progressBar.value = 0;
progressBar.style.display = 'none';
status.textContent = 'Upload completed successfully!';
} else {
alert('An error occurred while uploading files.');
}
};
// Handle errors
xhr.onerror = function() {
alert('An error occurred while uploading files.');
progressBar.value = 0;
progressBar.style.display = 'none';
status.textContent = 'Upload failed. Please try again.';
};
xhr.send(formData);
});
</script>
</body>
</html>