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?
How can I console.log() a Blob object?
Asked Answered
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* methods –
Konstanze
Updated for 2023, this can now be done with
await blob.text()
(Thanks @Kaiido)
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);
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
Updated for 2023, this can now be done with
await blob.text()
(Thanks @Kaiido)
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
});
He did not want to see base64. –
Farci
© 2022 - 2024 — McMap. All rights reserved.