优化OTA网页显示效果

This commit is contained in:
Yu-886 2025-03-17 17:02:28 +08:00
parent 3cebcde66c
commit 19103250d5

View File

@ -1,129 +1,165 @@
<!DOCTYPE html> <!DOCTYPE html>
<head> <head>
<meta charset='UTF-8'> <meta charset='UTF-8'>
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<title>在线配置</title> <title>在线配置</title>
<style type="text/css"> <style type="text/css">
* { margin: 0; padding: 0; } * { margin: 0; padding: 0; box-sizing: border-box; }
html { height: 100%; } html, body { height: 100%; background-color: #1F6F4A; display: flex; align-items: center; justify-content: center; }
h2 {text-align: center;color: #fff;line-height: 2.2;}
body { height: 100%; background-color: #1F6F4A; 50% 50% no-repeat; background-size: cover;}
.dowebok { position: absolute; left: 50%; top: 30%; width: 380px; height: 500px; margin: -200px 0 0 -200px; border: 3px solid #fff; border-radius: 10px; overflow: hidden;}
.form-item { position: relative; width: 360px; margin: 0 auto; padding-bottom: 20px;} h2, label { text-align: center; color: #fff; line-height: 2.2; }
.form-item input { width: 288px; height: 48px; padding-left: 10px; border: 1px solid #fff; border-radius: 25px; font-size: 18px; color: #fff; background-color: transparent; outline: none;}
.send_button { width: 360px; height: 50px; border: 0; border-radius: 25px; font-size: 18px; color: #1f6f4a; outline: none; cursor: pointer; background-color: #fff; }
.tip { display: none; position: absolute; left: 20px; top: 52px; font-size: 14px; color: #f50; } .dowebok {
.reg-bar { width: 360px; margin: 20px auto 0; font-size: 14px; overflow: hidden;} width: 380px;
.reg-bar a { color: #fff; text-decoration: none; } height: 500px;
.reg-bar a:hover { text-decoration: underline; } border: 3px solid #fff;
.reg-bar .reg { float: left; } border-radius: 10px;
.reg-bar .forget { float: right; } padding: 20px;
.dowebok ::-webkit-input-placeholder { font-size: 18px; line-height: 1.4; color: #fff;} text-align: center;
.dowebok :-moz-placeholder { font-size: 18px; line-height: 1.4; color: #fff;} }
.dowebok ::-moz-placeholder { font-size: 18px; line-height: 1.4; color: #fff;}
.dowebok :-ms-input-placeholder { font-size: 18px; line-height: 1.4; color: #fff;}
@media screen and (max-width: 500px) { .form-item {
* { box-sizing: border-box; } width: 100%;
.dowebok { position: static; width: auto; height: auto; margin: 0 30px; border: 0; border-radius: 0; } margin-bottom: 20px;
.logo { margin: 50px auto; } }
.form-item { width: auto; }
.form-item input, .form-item button, .reg-bar { width: 100%; }
}
/* 自定义文件上传区域 */
.custom-file {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
border: 1px solid #fff;
border-radius: 5px;
padding: 8px 12px;
}
.custom-file span {
color: white;
font-size: 16px;
flex-grow: 1;
text-align: left;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.custom-file label {
background-color: #fff;
color: #1F6F4A;
padding: 6px 12px;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
font-weight: bold;
white-space: nowrap;
}
.custom-file input {
display: none; /* 隐藏原始文件选择框 */
}
input[type="text"] {
width: 100%;
height: 40px;
border: 1px solid #fff;
border-radius: 5px;
padding-left: 10px;
color: #fff;
background-color: transparent;
outline: none;
}
button {
width: 100%;
height: 40px;
border: none;
border-radius: 5px;
background-color: #fff;
color: #1F6F4A;
font-size: 16px;
cursor: pointer;
}
button:hover {
background-color: #ddd;
}
/* 响应式调整 */
@media screen and (max-width: 500px) {
.dowebok { width: 90%; height: auto; padding: 10px; }
}
</style> </style>
</head> </head>
<body> <body>
<div class="dowebok"> <div class="dowebok">
<h2>ESP32S2 OTA Bin Server</h2> <h2>ESP32S2 OTA Bin Server</h2>
<!-- <table border="0">
</table> --> <!-- 自定义文件选择框 -->
<tr> <div class="form-item">
<td> <div class="custom-file">
<label for="newfile">Upload a file</label> <span id="file-name">未选择文件</span>
</td> <label for="newfile">选择文件</label>
<td colspan="2"> <input id="newfile" type="file" accept=".bin" onchange="updateFileName()">
<input id="newfile" type="file" accept=".bin" onchange="setpath()" style="width:100%;"> </div>
</td> </div>
</tr>
<tr> <div class="form-item">
<td> <label for="filepath">Set path on server</label>
<label for="filepath">Set path on server </label> <input id="filepath" type="text" readonly>
</td> </div>
<td>
<input id="filepath" type="text" style="width:100%;"> <button id="upload" type="button" onclick="upload()">Upload</button>
</td> <br><br>
<td> <button onclick="window.location.href='/'">返回</button>
<button id="upload" type="button" onclick="upload()">Upload</button>
</td>
</tr>
<!-- <form style='text-align: center;padding-top: 20px' action='/ota' method='POST'>
<td>
<button id="upload" type="button" onclick="upload()">Upload</button>
</td>
</form> -->
<form style='text-align: center;padding-top: 20px' name='input' action='/config' method='POST'>
<div class="form-item">
<div id="">
<input id="return_page" type='button' value='返回' onclick="location.href='/'">
</div>
</div>
</form>
</div> </div>
</body> </body>
<script> <script>
function setpath() { function updateFileName() {
var default_path = document.getElementById("newfile").files[0].name; var fileInput = document.getElementById("newfile");
document.getElementById("filepath").value = default_path; var fileNameSpan = document.getElementById("file-name");
} var filePathInput = document.getElementById("filepath");
function upload() {
var filePath = document.getElementById("filepath").value;
var upload_path = "/upload/" + filePath;
var fileInput = document.getElementById("newfile").files;
/* Max size of an individual file. Make sure this if (fileInput.files.length > 0) {
* value is same as that set in file_server.c */ var file = fileInput.files[0];
var MAX_FILE_SIZE = 20*1024*1024; var fileSize = formatFileSize(file.size);
var MAX_FILE_SIZE_STR = "20MB"; fileNameSpan.textContent = file.name;
filePathInput.value = `${file.name} (${fileSize})`;
if (fileInput.length == 0) {
alert("No file selected!");
} else if (filePath.length == 0) {
alert("File path on server is not set!");
} else if (filePath.indexOf(' ') >= 0) {
alert("File path on server cannot have spaces!");
} else if (filePath[filePath.length-1] == '/') {
alert("File name not specified after path!");
} else if (fileInput[0].size > MAX_FILE_SIZE) {
alert("File size must be less than " + MAX_FILE_SIZE_STR);
} else { } else {
document.getElementById("newfile").disabled = true; fileNameSpan.textContent = "未选择文件";
document.getElementById("filepath").disabled = true; filePathInput.value = "";
document.getElementById("upload").disabled = true;
var file = fileInput[0];
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4) {
if (xhttp.status == 200) {
document.open();
document.write(xhttp.responseText);
document.close();
} else if (xhttp.status == 0) {
alert("Server closed the connection abruptly!");
location.reload()
} else {
alert(xhttp.status + " Error!\n" + xhttp.responseText);
location.reload()
}
}
};
xhttp.open("POST", upload_path, true);
xhttp.send(file);
} }
} }
// 文件大小格式化
function formatFileSize(size) {
if (size < 1024) return size + " B";
else if (size < 1024 * 1024) return (size / 1024).toFixed(2) + " KB";
else return (size / (1024 * 1024)).toFixed(2) + " MB";
}
function upload() {
var fileInput = document.getElementById("newfile");
var filePath = document.getElementById("filepath").value;
if (fileInput.files.length === 0) {
alert("请先选择一个文件!");
return;
}
var formData = new FormData();
formData.append("file", fileInput.files[0]);
formData.append("path", filePath);
fetch("/upload", {
method: "POST",
body: formData,
headers: {
"Accept": "application/json"
}
}).then(response => response.text())
.then(data => alert("上传成功:" + data))
.catch(error => alert("上传失败:" + error));
}
</script> </script>