I need to pass a base 64 encoded string to javascript function and wants the function to display image in new tab. I’m generating the html dynamically from java and calling the JS function there.I’m getting syntax error as unexpected token, whenever i try to call that function by clicking that hyperlink.
My java code:
String base64Image = Base64.getEncoder().encodeToString(CscmFcnaRes1.getImage()).trim();
Dynamic html generated using stringBuffer:
.append("<A href="#"")
.append(" onClick=displayImage(")
.append(base64Image)
.append(")>"
JS function:
function displayImage(base64Image) {
var newTab = window.open();
newTab.document.open();
var isPdf = base64Image.substring(0, 4) === 'JVBER';
var mimeType = isPdf ? 'application/pdf' : 'image/jpeg';
var htmlContent = '<html><body>';
htmlContent += '<embed width="100%" height="100%" src="data:' + mimeType + ';base64,' + base64Image + '">';
htmlContent += '</body></html>';
newTab.document.write(htmlContent);
newTab.document.close();
}
I cant change anything in terms of bringing additional frameworks as its a prod running application. I need this image to be displayed in a new tab. When i manually decode the base64 string, I’m able to see the image. Note the base64 string is around 80k characters.