-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
94 lines (87 loc) · 2.8 KB
/
index.html
File metadata and controls
94 lines (87 loc) · 2.8 KB
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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>打印客户端</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
.container {
border: 1px solid #ccc;
padding: 20px;
border-radius: 5px;
}
button {
padding: 8px;
margin: 5px 0;
width: 100%;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.error {
color: red;
margin: 10px 0;
}
</style>
</head>
<body>
<div class="container">
<h2>打印客户端</h2>
<button id="uploadBtn">选择文件</button>
<input type="file" id="fileInput" style="display: none" accept=".pdf,.jpg,.jpeg,.png,.bmp">
<div id="errorMessage" class="error"></div>
</div>
<script>
const API_BASE_URL = 'http://127.0.0.1:5000/api';
// 文件上传处理
document.getElementById('uploadBtn').addEventListener('click', () => {
document.getElementById('fileInput').click();
});
document.getElementById('fileInput').addEventListener('change', async (event) => {
const file = event.target.files[0];
if (file) {
await uploadFile(file);
}
});
// 上传文件到服务器
async function uploadFile(file) {
try {
const formData = new FormData();
formData.append('file', file);
const response = await fetch(`${API_BASE_URL}/print`, {
method: 'POST',
body: formData
});
const result = await response.json();
if (result.error) {
showError(result.error);
} else {
alert('文件已上传,请在弹出的预览窗口中完成打印设置');
// 清空文件选择
document.getElementById('fileInput').value = '';
}
} catch (error) {
showError('上传失败,请检查打印服务是否正常运行');
}
}
function showError(message) {
const errorElement = document.getElementById('errorMessage');
errorElement.textContent = message;
setTimeout(() => {
errorElement.textContent = '';
}, 5000);
}
</script>
</body>
</html>