How can I console.log() a Blob object?
Asked Answered
G

3

15

I have a Blob object I want to inspect by logging its value. All I can see are type and size properties. Is there a way to do this?

console.logging a blob shows this

Goniometer answered 30/12, 2014 at 18:12 Comment(3)
What browser are you using?Featherweight
I'm using Chrome 39 (latest as of now)Goniometer
You cannot directly view the data inside a blob object, you have to use something like FileReader's readDataAs* methodsKonstanze
G
1

Updated for 2023, this can now be done with

await blob.text()

(Thanks @Kaiido)

Goniometer answered 3/4, 2023 at 13:24 Comment(0)
H
20

Basic example on using a FileReader to look at the content in a blob

var html= ['<a id="anchor">Hello World</a>'];
var myBlob = new Blob(html, { type: 'text/xml'});
var myReader = new FileReader();
myReader.onload = function(event){
    console.log(JSON.stringify(myReader.result));
};
myReader.readAsText(myBlob);
Hymnal answered 30/12, 2014 at 18:26 Comment(2)
Can be utterly shortened with await blob.text() nowadays.Barbey
@Barbey thanks for that updated answer - I've just posted that and changed the accepted answer.Goniometer
G
1

Updated for 2023, this can now be done with

await blob.text()

(Thanks @Kaiido)

Goniometer answered 3/4, 2023 at 13:24 Comment(0)
L
-2

First of all we should create a function for converting blob to base64:

const blobToBase64 = blob => {
  const reader = new FileReader();
  reader.readAsDataURL(blob);
  return new Promise(resolve => {
    reader.onloadend = () => {
      resolve(reader.result);
    };
  });
};

Then we can use this function to use it for console.log:

blobToBase64(blobData).then(res => {
  console.log(res); // res is base64 now
  // even you can click on it to see it in a new tab
});

Lanlana answered 13/12, 2021 at 7:4 Comment(1)
He did not want to see base64.Farci

© 2022 - 2024 — McMap. All rights reserved.