Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Base64 and AJAX

 
Bob Grossman
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pretty odd way of doing things. Why don't you just change the src attribute of the image?
 
Bob Grossman
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic