-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
55 lines (47 loc) · 1.98 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>File Upload</title>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<input type="file" id="fileInput" />
<button onclick="uploadFile()">Upload</button>
<div>
<progress id="uploadProgress" value="0" max="100" style="width: 100%"></progress>
<span id="uploadPercentage">0%</span>
</div>
<script>
const uploadFile = async () => {
const fileInput = document.getElementById('fileInput')
// เพิ่ม 2 อันนี้เข้ามา
const progressBar = document.getElementById('uploadProgress')
const uploadPercentageDisplay = document.getElementById('uploadPercentage')
const formData = new FormData()
formData.append('file', fileInput.files[0])
if (!fileInput.files.length) {
return alert('Please choose a file to upload')
}
try {
const response = await axios
.post('http://localhost:8000/upload', formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
onUploadProgress: function (progressEvent) {
// เพิ่ม update progress กลับเข้า UI ไป
const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
progressBar.value = percentCompleted
uploadPercentageDisplay.innerText = `${percentCompleted}%`
},
})
} catch (error) {
console.log('error', error)
alert('Error uploading file')
}
}
</script>
</body>
</html>