Response.blob()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.
句法
js
response.blob().then(function (myBlob) {
// do something with myBlob
});
参数
None.
返回值
A promise that resolves with a Blob
.
例子
在我们 fetch request example (run fetch request live) 中,我们使用Request.Request构造方法创建了一个新的 request 对象,然后使用它来获取一个 JPG 文件。当 fetch 成功的时候,我们使用 blob() 从 response 中读取一个Blob对象,并使用URL.createObjectURL 将它放入一个 object URL,然后把 URL 设置为img元素的 src 属性以显示这张图片。
js
var myImage = document.querySelector("img");
var myRequest = new Request("flowers.jpg");
fetch(myRequest)
.then(function (response) {
return response.blob();
})
.then(function (myBlob) {
var objectURL = URL.createObjectURL(myBlob);
myImage.src = objectURL;
});
规范
Specification |
---|
Fetch Standard # ref-for-dom-body-blob① |
浏览器兼容性
BCD tables only load in the browser