tags:

views:

219

answers:

2
+1  Q: 

gwt base64 image

Hi

I am getting a base64 byte[] from an xml file via jaxb and I am not sure how to convert this back to an gwt image (which is basically an underlying html img if I understood that correctly). How do I convert into the proper string?

My first instinct was to

public void onSuccess(final byte[] icon) {
img.setUrl("data:image/png;base64,"+icon.toString());

but obviously that does not work. Any help is appreciated!

A: 

You should supply the URL which can be used to get the image. I really don't think your code will result in something that looks like a url (something like, http://localhost/myimage.png, or maybe mywebapp/myimage.png...)

markovuksanovic
The OP is talking about data URIs. The format is valid: http://en.wikipedia.org/wiki/Data_URI_scheme#Format (note, that it doesn't work with all browsers)
Chris Lercher
You're right... :)
markovuksanovic
A: 

If you want to use data URIs (with base64 encoding) - although IE <=7 doesn't support it, and IE8 only allows up to 32 kB - you'll have to base64-encode the image data.

There are several Base64 encoders around e.g. com.google.gwt.user.server.Base64Utils, which you can use on the server side:

String base64 = Base64Utils.toBase64(icon);

Then transfer the encoded data to the client.

If you absolutely want to, you could also use the encoder on the client side, but that would require to copy the java file to the client source (if you make sure that the implementation you choose allows that).

Chris Lercher