I have a Java servlet which I can use to download files from a server, that works perfectly fine. Now I would like to use this servlet to display images on the website. After requesting an image, the Java script function viewImage gets applied and the image is transmitted in the variable response. The function viewImage looks as follows:
function viewImage(response)
{
// convert to Base64
let b64Image = btoa(unescape(encodeURIComponent(response)));
// create image
let imageData = document.createElement('img');
imageData.src = 'data:image/png;base64,' + b64Image;
// append to site
this.documentViewerDiv.appendChild(imageData);
};
The variable response with the image data takes, for example, values like:
�PNG
\u001a
\u0000\u0000\u0000
IHDR\u0000\u0000\u0000#\u0000\u0000\u0000%\u0008\u0006\u0000\u0000\u0000ȀPD\u0000\u0000\u0000\u0001sRGB\u0000��\u001c�\u0000\u0000\u0000\u0004gAMA\u0000\u0000��\u000b�a\u0005\u0000\u0000\u0000 pHYs\u0000\u0000\u000e�\u0000\u0000\u000e�\u0001�o�d\u0000\u0000\u0000>IDATXG��!\u0001\u0000 \u0000�0��\u0005\u0002@\u0001�\u0011\u0013�\u001bs��\u000b�\"Sd�L�)2E��\u0014�\"Sd�L�)2E��\u0014�\"��\u00051\\c�W\\��\u0000\u0000\u0000\u0000IEND�B`�
The response doesn't look too bad, does it? Well, the conversion still does not work properly and I don't know what's wrong. Does anyone have an idea where my mistake is?