Response: blob() method
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.
Note: This feature is available inWeb Workers.
Theblob() method of theResponse interface takesaResponse stream and reads it to completion. It returns a promise thatresolves with aBlob.
In this article
Syntax
blob()Parameters
None.
Note:If theResponse has aResponse.type of"opaque", the resultingBlobwill have aBlob.size of0 and aBlob.type ofempty string"", which renders ituseless for methods likeURL.createObjectURL().
Return value
A promise that resolves with aBlob whose data is the body's bytes and the media type is the response'sContent-Type header's value.
Exceptions
AbortErrorDOMExceptionThe request wasaborted.
TypeErrorThrown for one of the following reasons:
- The response body isdisturbed or locked.
- There was an error decoding the body content (for example, because the
Content-Encodingheader is incorrect).
Examples
In ourfetch request example (runfetch request live), wecreate a new request using theRequest() constructor,then use it to fetch a JPG. When the fetch is successful, we read aBlobout of the response usingblob(), put it into an object URL usingURL.createObjectURL(), and then set that URL as the source of an<img> element to display the image.
const myImage = document.querySelector("img");const myRequest = new Request("flowers.jpg");fetch(myRequest) .then((response) => response.blob()) .then((myBlob) => { const objectURL = URL.createObjectURL(myBlob); myImage.src = objectURL; });Specifications
| Specification |
|---|
| Fetch> # ref-for-dom-body-blob①> |