This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Binary Data  using AJAX Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Binary Data  using AJAX" Watch "Binary Data  using AJAX" New topic
Author

Binary Data using AJAX

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10142
    
165

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?


[My Blog] [JavaRanch Journal]
Dhanya Palanisamy
Ranch Hand

Joined: Oct 20, 2006
Posts: 61
The server can send back an XML file where you can put binary data inside CDATA section.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10142
    
165

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

Joined: Oct 20, 2006
Posts: 61
Hi Jai,

You have to encode your binary data before.

Dhanya
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Binary Data, are you talking about an image? What is this data?

Eric
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10142
    
165

That's right. By binary data i meant images that the server can send back to the client.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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.


Rahul Bhattacharjee
LinkedIn - Blog
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10142
    
165

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
Marshal

Joined: Jul 20, 2005
Posts: 10142
    
165

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
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Nov 29, 2005
Posts: 2308
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
Marshal

Joined: Jul 20, 2005
Posts: 10142
    
165

Thanks everyone for those explanations. Got the point
 
GeeCON Prague 2014
 
subject: Binary Data using AJAX