Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

网络请求-3-fetch 下载进度 勘误 #1246

Open
@frankli0324

Description

@frankli0324

原文链接:https://zh.javascript.info/fetch-progress

在Content-Encoding指定了某种压缩方式时,fetch提供的getReader中读取到的是解压缩的二进制流,而Content-Length所指示的是解压缩前的请求体大小。

在绝大多数场景,浏览器自动指示Accept-Encoding的情况下,不能直接使用Content-Length作为完整报文长度进行进度计算,而应当使用其它方式/响应头告知客户端完整的报文长度。

原文中示例代码如下:

// Step 1:启动 fetch,并获得一个 readerletresponse=awaitfetch('https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits?per_page=100');constreader=response.body.getReader();// Step 2:获得总长度(length)constcontentLength=+response.headers.get('Content-Length');// Step 3:读取数据letreceivedLength=0;// 当前接收到了这么多字节letchunks=[];// 接收到的二进制块的数组(包括 body)while(true){const{done, value}=awaitreader.read();if(done){break;}chunks.push(value);receivedLength+=value.length;console.log(`Received${receivedLength} of${contentLength}`)}// 以下略

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp