这篇翻译不完整。请帮忙从英语翻译这篇文章

Body mixin的只读getter属性 body 用于暴露其body内容的ReadableStream(流读取)。

语法

var stream = responseInstance.body;

Value

一个 ReadableStream.

例程

在我们的 simple stream pump 例程中我们fetch一个图片地址,使用response.body暴露响应的流,用ReadableStream.getReader()创建一个读取器,然后将其置入第二个自定义读取流中——有效的创建了一个完全相同的图片副本。

const image = document.getElementById('target');

// 请求原始图片
fetch('./tortoise.png')
// 取出body
.then(response => response.body)
.then(body => {
  const reader = body.getReader();

  return new ReadableStream({
    start(controller) {
      return pump();

      function pump() {
        return reader.read().then(({ done, value }) => {
          // 读不到更多数据就关闭流
          if (done) {
            controller.close();
            return;
          }

          // 将下一个数据块置入流中
          controller.enqueue(value);
          return pump();
        });
      }
    }
  })
})
.then(stream => new Response(stream))
.then(response => response.blob())
.then(blob => URL.createObjectURL(blob))
.then(url => console.log(image.src = url))
.catch(err => console.error(err));

规范

规范 状态 备注
Fetch
body
Living Standard  

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
body
Experimental
Chrome Full support 52Edge Full support YesFirefox Full support 65
Full support 65
Full support 57
Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true) and the javascript.options.streams preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 39Safari No support NoWebView Android Full support 52Chrome Android Full support 52Edge Mobile ? Firefox Android Full support 65
Full support 65
Full support 57
Disabled
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true) and the javascript.options.streams preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 39Safari iOS No support NoSamsung Internet Android Full support 6.0

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
User must explicitly enable this feature.
User must explicitly enable this feature.

 

相关链接

文档标签和贡献者

此页面的贡献者: jason-grimm, mdnwebdocs-bot, jcsahnwaldt, Axue
最后编辑者: jason-grimm,