This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Base64 and AJAX Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Base64 and AJAX" Watch "Base64 and AJAX" New topic
Author

Base64 and AJAX

Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
I'm experimenting with updating images in a Web page by using AJAX to send new image display options to a Java method on the server. The server generates a Base64-encoded URI for a PNG image and writes it to a JSP Page. AJAX returns this page, I extract the URI from it using standard Javascript string methods, and I use innerHTML to replace the old image (the entire image, not just the src) with the new one.

Sometimes this procedure gives me a ? instead of the image. In fact, in one particular case, I have an image that displays just fine, but if I change an option, then change it back to what it was originally, I get a ?, even though the image (and presumably the URI) hasn't changed.

So here's my question. Is it possible that some, but not all, Base64-encoded strings could become corrupted by being written to a text document, then extracted into a Javascript variable, then parsed with standard Javascript string methods?

I'm using a class called Base64Coder from http://www.source-code.biz to do the encoding into Base64.

Here's the src for the image that displays just fine:

[Edit: ultra-wide Base64 string removed]

And here's what the Java method is returning upon the AJAX request to revert to the original image:

[Edit: ultra-wide Base64 string removed]

Here's the Javascript method I use to update the image:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61082
    
  66

That's a pretty odd way of doing things. Why don't you just change the src attribute of the image?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
Because changing the src wasn't working either. Besides, I was originally using SVG to generate the image, so it was as little change as possible to drop in the entire tag in place of the SVG.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61082
    
  66

Well, SVG is an entirely different beast. But with the normal image tag, changing the source should work without issues. Likely the URL you were using was wrong if it didn't work for you.
Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
I agree. The question is, why is it wrong? Can the process of writing the base64 string to a text document cause information to be lost or corrupted?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61082
    
  66

I don't know -- not something I'd ever try to do. If there are line terminators involved, then yeah, that's sometime a problem.
 
jQuery in Action, 2nd edition
 
subject: Base64 and AJAX