-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.htm
143 lines (127 loc) · 4.28 KB
/
upload.htm
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
<!doctype HTML>
<html lang="en">
<head>
<title>ESP32 UPLOAD</title>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="icon" href="data:;base64,iVBORw0KGgo="> <!--prevent favicon requests-->
<style>
#uploadForm {
width:350px;
margin:0 auto;
background-color:beige;
text-align:center;
border:solid 1px black;
}
#title{
text-align:center;
margin:5px;
}
#fileSelection{
height:50px;
border:solid 1px black;
}
#uploadProgressBar{
margin:10px;
}
#uploadFileButton{
width:100px;
height:30px;
margin:10px;
}
#statusText{
margin:5px;
}
#credentials{
margin:2px;
display:flex;
}
#username, #password {
width:48%;
margin:5px;
text-align:center;
}
</style>
</head>
<body>
<form id="uploadForm" enctype="multipart/form-data">
<p id="title">Simple authenticated upload example<br>for ESP32 and ESPAsyncWebServer</p>
<p id="credentials">
<input type="text" id="username" name="username" placeholder="username">
<input type="password" id="password" name="password" placeholder="password">
</p>
<input id="fileSelection" name="file" type="file" />
<progress id="uploadProgressBar" value="0" max="0"></progress>
<p id="statusText"> </p>
<input id="uploadFileButton" type="submit" value="Upload" disabled="disabled"/>
</form>
<script type="text/javascript">
function startUpload() {
let request = new XMLHttpRequest();
request.onerror = function() {
window.statusText.innerHTML = 'ERROR: http request failed!';
}
request.ontimeout = function() {
window.statusText.innerHTML = 'ERROR: Connection timeout!';
}
request.onreadystatechange = function() {
if (this.status == 404) {
window.statusText.innerHTML = '404 - Upload URL not found on server';
window.fileSelection.value = '';
}
if (this.readyState == 4) {
switch (this.status) {
case 200 :
window.statusText.innerHTML = 'Upload succes!';
window.fileSelection.value = '';
window.fileSelection.disabled = false;
window.uploadFileButton.disabled = true;
break;
case 400 :
window.statusText.innerHTML = this.responseText;
window.fileSelection.disabled = false;
break;
case 401 :
window.statusText.innerHTML = 'Enter valid username and password';
window.fileSelection.disabled = false;
window.uploadFileButton.disabled = false;
break;
default : console.log('http result code: ' + this.status);
}
}
};
request.upload.addEventListener('progress', function(event) {
const percent = (event.loaded / event.total) * 100;
window.statusText.innerHTML = Math.round(percent) + '% uploaded. Please wait...';
let progress = window.uploadProgressBar;
progress.setAttribute('value', event.loaded);
progress.setAttribute('max', event.total);
});
request.addEventListener('load', function() {
let progress = window.uploadProgressBar;
progress.setAttribute('value', 0);
progress.setAttribute('max', 0);
});
request.open('POST', '/');
//request.open('POST', 'http://192.168.0.107/'); //debug/remote running of this page
request.setRequestHeader('Authorization', 'Basic ' + btoa(window.username.value + ":" + window.password.value));
const file = window.fileSelection.files[0];
request.setRequestHeader('FileSize', file.size);
let data = new FormData();
data.append('file', file);
window.statusText.innerHTML = 'Initializing upload...';
request.send(data);
}
document.getElementById('fileSelection').addEventListener('change', function() {
window.uploadFileButton.disabled = this.files[0] ? false : true;
window.statusText.innerHTML = ' ';
});
document.getElementById('uploadFileButton').addEventListener('click', function(event){
event.preventDefault();
this.disabled = true;
window.fileSelection.disabled = true;
startUpload();
});
</script>
</body>
</html>