?
Solved

Binary response from webservice, convert to text?

Posted on 2011-04-28
2
Medium Priority
?
825 Views
Last Modified: 2012-05-11
I have access to a webservice that returns binary data. I have set up an ajax request that returns text like the following,

(q/¿M¿ ¿¿¿&PB¿¿¿¿p/¿M A¿¿&PB¿¿¿¿¿=¿MH¿¿&PB¿¿¿¿¿K¿M 2¿¿¿PB¿¿¿¿p/¿M J¿¿¿PB¿¿¿¿¿=¿M G¿¿¿PB¿¿¿¿¿K¿M B¿SYQB¿I¿¿p/¿M T¿

How do I use this data? Is the browser already interpreting it? I can request the data as json but their is so much of it that the request takes >60seconds so we are trying to transfer the data in binary and interpret it on the client. This transfer is an order of magnitude faster.
function loadXMLDoc(){
	var url = "http://10.10.10.10/pa/pa/pd";
	var params = "times=1303916400%2C0%2C0&timerange=0..2&areas=53%2C-8%2C52%2C-8%2C54%2C-8&ndp=1%2C2%2C6%2C7%2C14%2C17%2C18%2C19&server=10.50.50.10&command=grid&obs=";
	
	var xmlhttp = new XMLHttpRequest();

	xmlhttp.onreadystatechange = function(){
	
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
		}
	}


	xmlhttp.open("POST", url, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(params);

}

Open in new window

0
Comment
Question by:OwenMoriarty
  • 2
2 Comments
 
LVL 2

Expert Comment

by:Gk
ID: 35482515
var xhr = new XMLHttpRequest();
02
xhr.open("GET", "binary_file", false);
03
xhr.send(null);
04
 
05
buffer = xhr.mozResponseArrayBuffer;
06
if (buffer) {
07
  var byteArray = new Uint8Array(buffer);
08
  for (var i=0; i<byteArray.byteLength; i++) {
09
    // do something with each byte in the array
10
  }
11
}
https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest#Receiving_binary_data



refer :
0
 
LVL 2

Accepted Solution

by:
Gk earned 2000 total points
ID: 35482530
function load_binary_resource(url) {  
  var req = new XMLHttpRequest();  
  req.open('GET', url, false);  
  //XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]  
  req.overrideMimeType('text/plain; charset=x-user-defined');  
  req.send(null);  
  if (req.status != 200) return '';  
  return req.responseText;  
}  
The magic happens in line 5, which overrides the MIME type, forcing Firefox to treat it as plain text, using a user-defined character set.  This tells Firefox not to parse it, and to let the bytes pass through unprocessed.
Refer : http://www1.icyte.com/system/snapshots/fs1/8/5/c/d/85cdb5f77f163918992f0efeaa5b6f3f5008deed/index.html#Receiving_binary_data_using_JavaScript_typed_arrays
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
#Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question