-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
47 lines (46 loc) · 1.44 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图片上传</title>
<style>
/* 添加样式 */
.upload-form {
width: 300px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
.upload-form input[type="file"], .upload-form button {
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="upload-form">
<input type="file" id="image-upload" accept="image/*">
<button type="submit">上传</button>
</div>
<script>
document.getElementById('image-upload').addEventListener('change', function(e) {
const file = e.target.files[0]; // 获取选中的文件
const formData = new FormData(); // 创建FormData对象
formData.append('image', file); // 将文件添加到FormData对象中
// 发送POST请求到后端服务器
fetch('/upload', { // 使用fetch API发送请求
method: 'POST', // 使用POST方法提交请求
body: formData // 将FormData对象作为请求体发送
})
.then(response => response.json()) // 将响应解析为JSON格式
.then(data => {
// 处理服务器返回的数据,例如显示上传成功的信息等
console.log(data);
})
.catch(error => {
// 处理请求过程中发生的错误
console.error(error);
});
});
</script>
</body>
</html>