tags:

views:

971

answers:

2
+1  Q: 

Data URIs in GWT

Is it possible to create data URI's in GWT?

I want to inject a byte array image as an actual image using a data URI.

Thanks!

+1  A: 

Yes. It is completely possible to do this. I'd done it for an application until I realized IE6 doesn't handle binary data streams this way. You can do it in several ways. For the purposes of my example, I'm already assuming that you've converted the byte array to a string somewhere, and that it is properly encoded and of the proper type for your data URI. I'm also assuming you know the basic format (or can find it) of your chosen data scheme.

I've taken these examples from the Wikipedia article on data URI scheme.

The first is to just use raw HTML to make the image reference as you normally would and have it inserted into the page.

HTML html = new HTML("<img src=\"data:image/png;base64,
iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGP
C/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IA
AAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1J
REFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jq
ch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0
vr4MkhoXe0rZigAAAABJRU5ErkJggg==\" alt=\"Red dot\">");

You can also just use an image. (Which should produce roughly the same output HTML/JS.)

Image image = new Image("data:image/png;base64,
iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGP
C/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IA
AAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1J
REFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jq
ch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0
vr4MkhoXe0rZigAAAABJRU5ErkJggg==");

This allows you to use the full power of the Image abstraction on top of your loaded image.

I'm still thinking that you may want to expand on this solution and use GWT's deferred binding mechanism to deal with browsers that do not support data URIs. (IE6,IE7)

angryundead
+2  A: 

You should checkout ClientBundle in GWT's trunk. It will create data urls automatically for browsers that support them and fallbacks for that other browser: http://code.google.com/p/google-web-toolkit/wiki/ClientBundle

The feature won't ship until GWT 2.0, but it's in heavy use now.

Kelly Norton
+1 for recommending clientBundle. Although, data URLs are perhaps less efficient in terms of bandwidth since base64 encoded strings is larger than the equivelant picture. I m not sure what data url has over CSS sprites (which ImageBundle already do automatically for you).
Chii
It really depends on the kind of data that your object model has. At work we interact with an XML data standard that has image binary data in it. It makes perfect sense to utilize this data on the client. Otherwise we have to break it out, strip it from the message, store it on the server somewhere, and then have a servlet call to go back and get the information. In other words: sometimes this beats having an image servlet.
angryundead
Thanks guys, this is PERFECT! :)