• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Binary Data using AJAX

 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been seing some sample examples of using AJAX and all of them show how to send back data from the server in the form of text or xml. I havent seen any example which shows how to send back binary data. Can AJAX be used to send back binary data?
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The server can send back an XML file where you can put binary data inside CDATA section.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i am not wrong, the CDATA section in xml can contain only text. I dont see how binary data can be passed as part of that.
 
Dhanya Palanisamy
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jai,

You have to encode your binary data before.

Dhanya
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Binary Data, are you talking about an image? What is this data?

Eric
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right. By binary data i meant images that the server can send back to the client.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you can use XmlHttpRequest for that. It returns either text or XML. You can encode binary data as text, but then your client-side JavaScript probably couldn't do anything useful with it.

However, if what you are trying to do is to dynamically update images, you do not need to send the image in this way.

If the IMG element is already part of the displayed HTML document, you can find it in the DOM and change its "src" attribute. This will cause the client to request the new binary image data from the server. That will be a background request which will not make the whole page refresh.

If you are wanting to add a new IMG to the displayed HTML document, you can add a new IMG element using DOM API and set its "src" appropriately. Again, this will cause the browser to request the binary image data from the server.

This assumes that you have a servlet or similar on the server that can handle the requests for binary image data.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jaikiran Pai:
I have been seing some sample examples of using AJAX and all of them show how to send back data from the server in the form of text or xml. I havent seen any example which shows how to send back binary data. Can AJAX be used to send back binary data?



AJAX can send binary data , but the question is what will you do with the binary data at client side.Mostly we use javascript for manupulating the data sent from the server , as far I know nothing much can be done with the binary data using javascript and if at all this can be done then also this would be very slow.

But as the question goes , You can take the binary chunk of data , then base64 encode that (so that it becomes fully printable) and send using a xml tag.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:

However, if what you are trying to do is to dynamically update images



Thats exactly what i wanted.

Originally posted by Peter Chase:
If the IMG element is already part of the displayed HTML document, you can find it in the DOM and change its "src" attribute. This will cause the client to request the new binary image data from the server. That will be a background request which will not make the whole page refresh.

If you are wanting to add a new IMG to the displayed HTML document, you can add a new IMG element using DOM API and set its "src" appropriately. Again, this will cause the browser to request the binary image data from the server.



I did not know that changing the "src" attribute would load the new image without loading the entire page in which the image is embedded.

Thanks, Peter Chase.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Bhattacharjee:

AJAX can send binary data , but the question is what will you do with the binary data at client side.



I agree plain binary data does not make sense for the client. I should have been more clear in my question, by binary data i meant images which i wanted to update without reloading the entire page.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As mentioned before, there is no need to even use Ajax to update an image when you can just set the source of the image to the serverside page. JavaScript can not handle binary data so you have to all of that stuff on the server. If you need to post data, I would make an Ajax post and return the url of the image in the responseText, than set the source of the image with that.

Eric
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jaikiran Pai:


I agree plain binary data does not make sense for the client. I should have been more clear in my question, by binary data i meant images which i wanted to update without reloading the entire page.



If you are aware of the image location then even AJAX is not required.I am sure that you must have noticed in many web sites where when you click a button (not an html input button rather a button made of html tables and images),then the button really looks like it has been clicked.This is something which they do by changing the images to a more blur image ,on the click.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for those explanations. Got the point
 
Screaming fools! It's nothing more than a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic