-1

I have binary data that I want to show as a preview in my modal

The binary data is as seen in the below image in Response:-

enter image description here

The same data is seen in preview:- enter image description here

If I hardcode the preview value in code then it works. But if I take the response from API then it doesn't work.

How to get the preview as seen in the preview image without hardcode ?

This works:

clickedFile.image = "/9j/4AAQSkZJRgABAQEASABIAAD/4QHDRXhpZ.."
        var myBase64 = "data:"+file.fileType+";base64,"+clickedFile.image+"";
        var img = new Image();
        img.src = myBase64;

But this doesn't works:

 clickedFile.image = api.response;
            var myBase64 = "data:"+file.fileType+";base64,"+clickedFile.image+"";
            var img = new Image();
            img.src = myBase64;

So how to convert binary response api.response to its preview value "/9j/4AAQSkZJRgABAQEASABIAAD/4QHDRXhpZ.."?

UPDATE:

Using REST Client, I can see the following dialogue: CANNOT PREVIEW IMAGE

enter image description here

sjain
  • 23,126
  • 28
  • 107
  • 185

1 Answers1

0

If an image is to be sent over ajax you need to base64 encode the bytes first. This is because ajax can only receive particular formats and binary is not one of them. The easiest way to do this is to make a data url on the server that you can simply set to the image.src. Be aware however that this requires more bandwidth than setting the image.src to a served file.

N-ate
  • 6,051
  • 2
  • 40
  • 48