HostMain/web/index.html
2024-12-31 10:03:07 +08:00

56 lines
2.2 KiB
HTML
Executable File

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DBC2C</title>
</head>
<body>
<h1>dbc转换为C代码</h1>
<p>上传dbc文件。</p>
<form id="uploadForm" enctype="multipart/form-data">
<input type="file" name="file" id="fileInput">
<input type="submit" value="上传">
</form>
<p id="status"></p>
<p id="responseInfo"></p> <!-- 用于显示API返回的结果 -->
<p id="errorMessage" style="color: red;"></p> <!-- 用于显示错误信息 -->
<script>
document.getElementById('uploadForm').addEventListener('submit', async function(event) {
event.preventDefault(); // 阻止表单提交
const formData = new FormData();
const fileInput = document.getElementById('fileInput');
formData.append("file", fileInput.files[0]);
// 显示上传状态
document.getElementById("status").textContent = "Uploading...";
// 上传文件
const response = await fetch('/api/uploadfile/', {
method: 'POST',
body: formData
});
const data = await response.json();
// 获取原始响应内容并显示
//const rawResponse = data.text(); // 获取原始文本响应
// 将 JSON 数据转换为文本(字符串)
const dataText = JSON.stringify(data, null, 2); // 格式化为带缩进的文本
document.getElementById("responseInfo").textContent = dataText;
if (response.ok) {
document.getElementById("status").textContent = "File uploaded successfully! Waiting to download...";
// 上传成功后,等待 5 秒再开始下载
setTimeout(() => {
// 延时后下载文件
window.location.href = `/api/download/CANmatrix.zip`; // 自动下载
}, 3000); // 3 秒
} else {
document.getElementById("status").textContent = `Error: ${data.error}`;
}
});
</script>
</body>
</html>