Javascript - I created a blob from a string, how do I get the string back out? Javascript - I created a blob from a string, how do I get the string back out? javascript javascript

Javascript - I created a blob from a string, how do I get the string back out?


In order to extract data from a Blob, you need a FileReader.

var reader = new FileReader();reader.onload = function() {    alert(reader.result);}reader.readAsText(blob);


@joey asked how to wrap @philipp's answer in a function, so here's a solution that does that in modern Javascript (thanks @Endless):

const text = await new Response(blob).text()


If the browser supports it, you could go via a blob URI and XMLHttpRequest it

function blobToString(b) {    var u, x;    u = URL.createObjectURL(b);    x = new XMLHttpRequest();    x.open('GET', u, false); // although sync, you're not fetching over internet    x.send();    URL.revokeObjectURL(u);    return x.responseText;}

Then

var b = new Blob(['hello world']);blobToString(b); // "hello world"