javascript-tutorial / javascript-tutorial/zh.javascript.info
网络请求-3-fetch 下载进度 勘误
还没有人认领这个 Issue。
- 主要语言
- HTML
- 星标
- 10.8k
- 派生
- 1.2k
- PR 合并指标
- 30 天内没有已合并 PR
描述
原文链接:https://zh.javascript.info/fetch-progress
在Content-Encoding指定了某种压缩方式时,fetch提供的getReader中读取到的是解压缩*后*的二进制流,而Content-Length所指示的是解压缩前的请求体大小。
在绝大多数场景,浏览器自动指示Accept-Encoding的情况下,不能直接使用Content-Length作为完整报文长度进行进度计算,而应当使用其它方式/响应头告知客户端完整的报文长度。
原文中示例代码如下:
```js
// Step 1:启动 fetch,并获得一个 reader
let response = await fetch('https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits?per_page=100');
const reader = response.body.getReader();
// Step 2:获得总长度(length)
const contentLength = +response.headers.get('Content-Length');
// Step 3:读取数据
let receivedLength = 0; // 当前接收到了这么多字节
let chunks = []; // 接收到的二进制块的数组(包括 body)
while(true) {
const {done, value} = await reader.read();
if (done) {
break;
}
chunks.push(value);
receivedLength += value.length;
console.log(`Received ${receivedLength} of ${contentLength}`)
}
// 以下略
```
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
Start with the fetch progress article at https://zh.javascript.info/fetch-progress and review the example using response.body.getReader(), Content-Length, and receivedLength. Correct the explanation or example so it accounts for compressed responses, and confirm that the documented progress calculation matches the byte stream returned by fetch.
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript
- 领域
- documentation
- Issue 类型
- 文档
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100