0

I am making https get a request to get the image and trying to show that image into the desktop app, but I am getting a weird binary response of the image, like diamonds, symbols etc. But I am unable to convert that to base64, but if I give absolute path of the image into the image src tag then it renders fine, but this has an issue with the server authentication process, so I need to make https call and show the image. I have attached the response of the image, can anyone help me how to decode this string and show the image in JavaScript?

Text containing all weird symbols, starting with "???? ?JIFF"

Code to decode: angular ajax, response type is set as "binary"

const length = response.length;
const array = new Uint8Array(length);
for (let i = 0; i < length; i++){
    array[i] = response.charCodeAt(i);
}
const u8 = new Uint8Array(array);
const b64encoded = btoa(String.fromCharCode.apply(null, u8));
Ferrybig
  • 18,194
  • 6
  • 57
  • 79
Shoib Mohammed A
  • 298
  • 4
  • 12
  • Can you show the code you use to download the binary blob, and the code you tried using to convert it to a Base64 url? It allows us to build our answer on knowledge you already have, and in turn makes it easier to understand for you. Thanks – Ferrybig Mar 01 '19 at 07:41
  • for now what I did is, I changed the responseType parameter value to "blob", from there I converted to "URL.createObjectURL(blob)" which worked, but now problem is, it also returns blob for base64 encoded response string, now i have the challenge to handle base64 & binary response for the same https get request – Shoib Mohammed A Mar 01 '19 at 09:33
  • Are you able to use the new `fetch` api? When you use fetch, you first get a "response" object that contains all headers and the status code, and you can basically add an `if` on it to either call `.json()` or `.blob()` depending on what you need. An image only example of this is here: https://stackoverflow.com/a/50248437/1542723 – Ferrybig Mar 01 '19 at 10:00
  • yeah @Ferrybig, I used the "if" condition on the response headers, based on that I am constructing the logic. – Shoib Mohammed A Mar 05 '19 at 12:35

0 Answers0