这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
body
只读特性在
Body
mixin is a simple getter used to expose a
ReadableStream
of the body contents.
var stream = response.body;
A
ReadableStream
.
In our
simple stream pump
example we fetch an image, expose the response's stream using
response.body
, create a reader using
ReadableStream.getReader()
, then enqueue that stream's chunks into a second, custom readable stream — effectively creating an identical copy of the image.
const image = document.getElementById('target');
// Fetch the original image
fetch('./tortoise.png')
// Retrieve its body as ReadableStream
.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 }) => {
// When no more data needs to be consumed, close the stream
if (done) {
controller.close();
return;
}
// Enqueue the next data chunk into our target stream
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
The definition of 'body' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
body
|
Chrome 52 | Edge ≤18 |
Firefox
65
|
IE No | Opera 39 | Safari 10 | WebView Android 52 | Chrome Android 52 |
Firefox Android
65
|
Opera Android 41 | Safari iOS Yes | Samsung Internet Android 6.0 |
完整支持
不支持
实验。期望将来行为有所改变。
用户必须明确启用此特征。