Skip to content

Commit b10aa01

Browse files
committed
feat: 2021.06.07
1 parent d5b78d3 commit b10aa01

13 files changed

Lines changed: 180 additions & 0 deletions

File tree

Cute-Gist/HTTP/.DS_Store

0 Bytes
Binary file not shown.
6 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
学习文章:[HTTP 传输大文件的几种方案](https://mp.weixin.qq.com/s/ZxVD29AYHG7gUL99D8uEpw)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<html lang="zh-cn">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>大文件范围请求示例(阿宝哥)</title>
8+
</head>
9+
<body>
10+
<h3>大文件范围请求示例(阿宝哥)</h3>
11+
<div id="msgList"></div>
12+
<script>
13+
const msgList = document.querySelector("#msgList");
14+
function getBinaryContent(url, start, end, responseType = "arraybuffer") {
15+
return new Promise((resolve, reject) => {
16+
try {
17+
let xhr = new XMLHttpRequest();
18+
xhr.open("GET", url, true);
19+
xhr.setRequestHeader("range", `bytes=${start}-${end}`);
20+
xhr.responseType = responseType;
21+
xhr.onload = function () {
22+
resolve(xhr.response);
23+
};
24+
xhr.send();
25+
} catch (err) {
26+
reject(new Error(err));
27+
}
28+
});
29+
}
30+
31+
getBinaryContent(
32+
"http://localhost:3000/big-file.txt",
33+
0, 100, "text"
34+
).then((text) => {
35+
msgList.append(`${text}`);
36+
});
37+
</script>
38+
</body>
39+
</html>
6 KB
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const fs = require("fs");
2+
3+
const writeStream = fs.createWriteStream(__dirname + "/big-file.txt");
4+
for (let i = 0; i <= 1e5; i++) {
5+
writeStream.write(`${i} 我是阿宝哥,欢迎关注全栈修仙之路\n`, "utf8");
6+
}
7+
8+
writeStream.end();

Cute-Gist/HTTP/write-larger-file-transform/server/index.js

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "server",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"dev": "node ."
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"devDependencies": {
14+
"@koa/cors": "^3.1.0",
15+
"@koa/multer": "^3.0.0",
16+
"@koa/router": "^10.0.0",
17+
"accepts": "^1.3.7",
18+
"fs-extra": "^10.0.0",
19+
"koa": "^2.13.1",
20+
"koa-static": "^5.0.0",
21+
"multer": "^1.4.2",
22+
"util": "^0.12.4"
23+
}
24+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const fs = require("fs");
2+
const http = require("http");
3+
4+
const buffer = fs.readFileSync(__dirname + "/big-file.txt");
5+
const lines = buffer.toString("utf-8").split("\n");
6+
const chunks = chunk(lines, 10);
7+
8+
function chunk(arr, len) {
9+
let chunks = [],
10+
i = 0,
11+
n = arr.length;
12+
while (i < n) {
13+
chunks.push(arr.slice(i, (i += len)));
14+
}
15+
return chunks;
16+
}
17+
18+
// 省略数据分块代码
19+
http
20+
.createServer(async function (req, res) {
21+
res.writeHead(200, {
22+
"Content-Type": "text/plain;charset=utf-8",
23+
"Transfer-Encoding": "chunked",
24+
"Access-Control-Allow-Origin": "*",
25+
});
26+
for (let index = 0; index < chunks.length; index++) {
27+
setTimeout(() => {
28+
let content = chunks[index].join("&");
29+
res.write(`${content.length.toString(16)}\r\n${content}\r\n`);
30+
}, index * 1000);
31+
}
32+
setTimeout(() => {
33+
res.end();
34+
}, chunks.length * 1000);
35+
})
36+
.listen(3000, () => {
37+
console.log("app starting at port 3000");
38+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const fs = require("fs");
2+
const http = require("http");
3+
const util = require("util");
4+
const readFile = util.promisify(fs.readFile);
5+
6+
const server = http.createServer(async (req, res) => {
7+
res.writeHead(200, {
8+
"Content-Type": "text/plain;charset=utf-8",
9+
});
10+
const buffer = await readFile(__dirname + "/big-file.txt");
11+
res.write(buffer);
12+
res.end();
13+
});
14+
15+
server.listen(3000, () => {
16+
console.log("app starting at port 3000");
17+
});

0 commit comments

Comments
 (0)