• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Base64 and AJAX

 
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:

 
Marshal
Posts: 67449
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Marshal
Posts: 67449
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Marshal
Posts: 67449
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic