Skip to content

Commit 3c470a5

Browse files
committed
更新jquery案例配合原生的node http服务器实现文件上传
1 parent 9e6b0fc commit 3c470a5

2 files changed

Lines changed: 10255 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title></title>
7+
</head>
8+
9+
<body>
10+
<p>利用二进制blob,配合node的http和fs模块实现上传文件</p>
11+
<p>后续还可以使用blob.slice方法实现分块上传</p>
12+
<input type="file" id="file" name="logo" />
13+
</body>
14+
<script src="jquery.js"></script>
15+
<script>
16+
var fileNode = document.getElementById("file");
17+
fileNode.onchange = function () {
18+
$.ajax({
19+
url: "http://localhost:8877",
20+
type: "POST",
21+
dataType: "binary",
22+
data: fileNode.files[0],
23+
headers: {
24+
'Content-Type': 'application/octet-stream',
25+
'X-Requested-With': 'XMLHttpRequest'
26+
},
27+
processData: false,
28+
}).done(function (data) {
29+
console.log(data);
30+
})
31+
fileNode.value = null;
32+
}
33+
</script>
34+
35+
</html>

0 commit comments

Comments
 (0)