I would like to convert a PDF to base64 and show on browser.
The problem is , the following code works for Firefox and Chrome
Note: For IE and other browsers like Mozilla, Chrome this works for me
if (data == "" || data == undefined) {
alert("Falied to open PDF.");
} else {
//For IE using atob convert base64 encoded data to byte array
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
var byteCharacters = atob(data);
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
var blob = new Blob([byteArray], {
type: 'application/pdf'
});
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {
// Directly use base 64 encoded data for rest browsers (not IE)
var base64EncodedPDF = data;
var dataURI = "data:application/pdf;base64," + base64EncodedPDF1;
window.open(dataURI, '_blank');
}
}