popo joe
popo joe

Reputation: 910

Send base64 string as image to the client

I managed to save an image as a base64 encoded string in my database, but I was wondering how tu serve the base64 string in a way that it is interpreted as an image on the client side.

i made a WS that returns the string like this :

return ok(myBase64String).as("image/jpeg")

but the image cannot be displayed in the browser.

if I decode the string and then send the byte array the image is displayed, now what i dont understand is why do I have to decode my already encoded image in order to display it in the client??

byte [] test =  Base64.decodeBase64(event.getPhoto());
                return ok(test).as("image/jpeg");

this works but why do I have to decode my base 64 string??

anyone have an idea? thanks!!

Upvotes: 1

Views: 2176

Answers (1)

Marius Soutier
Marius Soutier

Reputation: 11284

If you want to use Base64, you need to use the data uri scheme:

<img src="data:image/png;<base64>" />

Otherwise you have to pass in a path to a binary representation. That's what the Assets controller does, serving a file as binary.

Upvotes: 1

Related Questions