--- title: Body.blob() slug: Web/API/Body/blob translation_of: Web/API/Body/blob ---
Die Methode blob()
des {{domxref("Body")}} Mixin nimmt einen {{domxref("Response")}} Stream und liest ihn bis zum Ende. Sie gibt ein Promise zurück, welches in einen {{domxref("Blob")}} aufgelöst wird.
response.blob().then(function(myBlob) { // mach etwas mit myBlob });
Keine.
"opaque"
ist, hat der resultierende {{domxref("Blob")}} eine {{domxref("Blob.size")}} von 0
und einen {{domxref("Blob.type")}} eines leeren Strings ""
, wodurch er für Methoden wie {{domxref("URL.createObjectURL")}} unbrauchbar wird.Ein Promise, welches in einen {{domxref("Blob")}} aufgelöst wird.
In unserem Beispiel für eine Fetch Anfrage (live ausführen) erstellen wir eine neue Anfrage mit dem {{domxref("Request.Request")}} Konstruktor und rufen dann ein JPG ab. Wenn der Abruf erfolgreich ist, lesen wir mit blob()
einen {{domxref("Blob")}} aus der Antwort, fügen ihn mit {{domxref("URL.createObjectURL")}} in eine Objekt-URL ein und legen diese URL als Quelle für das {{htmlelement("img")}} Element zum Anzeigen des Bildes fest.
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 | Status | Comment |
---|---|---|
{{SpecName('Fetch','#dom-body-blob','blob()')}} | {{Spec2('Fetch')}} |
{{Compat("api.Body.blob")}}