• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Base64 and AJAX

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic