convert blob to string

IE FIX

by ayyanarj

JavaScript

// Easy way
var blb = new Blob(["Lorem ipsum sit"], {type: "text/plain"});
var strValue = '';

new Response(blb).text().then(t => {
console.log(t);
});


// IE FIX
blb = new Blob(["Lorem ipsum sit - IE FIX"], {type: "text/plain"});
strValue = '';
const readText = (blobString) => {
  console.log(blobString);
}
// Since IE not supporting text(), we are using FileReader to convert the blob to string.
const showLocalError = this.showError;
const reader = new FileReader();

// this event get triggered once read from blob is completed. once loadend event triggered, we can retrieve the value.
reader.addEventListener('loadend', function () {
  if (reader && reader.result) {
	readText(reader.result.toString());
  } else {
	console.log('Error in converting blob to string');
  }
});
// using readAsText, we are start to reading the blob. Once reading completed, loadend event will get trigged 
reader.readAsText(blb);