esp32-off-Line-tools/main/wifi/www/upload_script.html

166 lines
4.7 KiB
HTML

<!DOCTYPE html>
<head>
<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">
<title>在线配置</title>
<style type="text/css">
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; background-color: #1F6F4A; display: flex; align-items: center; justify-content: center; }
h2, label { text-align: center; color: #fff; line-height: 2.2; }
.dowebok {
width: 380px;
height: 500px;
border: 3px solid #fff;
border-radius: 10px;
padding: 20px;
text-align: center;
}
.form-item {
width: 100%;
margin-bottom: 20px;
}
/* 自定义文件上传区域 */
.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>
</head>
<body>
<div class="dowebok">
<h2>ESP32S2 OTA Bin Server</h2>
<!-- 自定义文件选择框 -->
<div class="form-item">
<div class="custom-file">
<span id="file-name">未选择文件</span>
<label for="newfile">选择文件</label>
<input id="newfile" type="file" accept=".bin" onchange="updateFileName()">
</div>
</div>
<div class="form-item">
<label for="filepath">Set path on server</label>
<input id="filepath" type="text" readonly>
</div>
<button id="upload" type="button" onclick="upload()">Upload</button>
<br><br>
<button onclick="window.location.href='/'">返回</button>
</div>
</body>
<script>
function updateFileName() {
var fileInput = document.getElementById("newfile");
var fileNameSpan = document.getElementById("file-name");
var filePathInput = document.getElementById("filepath");
if (fileInput.files.length > 0) {
var file = fileInput.files[0];
var fileSize = formatFileSize(file.size);
fileNameSpan.textContent = file.name;
filePathInput.value = `${file.name} (${fileSize})`;
} else {
fileNameSpan.textContent = "未选择文件";
filePathInput.value = "";
}
}
// 文件大小格式化
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>