Binary response from webservice, convert to text?

Posted on 2011-04-28
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 = "";
	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=";
	var xmlhttp = new XMLHttpRequest();

	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
	}"POST", url, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");


Open in new window

Question by:OwenMoriarty
    LVL 2

    Expert Comment

    var xhr = new XMLHttpRequest();
    02"GET", "binary_file", false);
    buffer = xhr.mozResponseArrayBuffer;
    if (buffer) {
      var byteArray = new Uint8Array(buffer);
      for (var i=0; i<byteArray.byteLength; i++) {
        // do something with each byte in the array

    refer :
    LVL 2

    Accepted Solution

    function load_binary_resource(url) {  
      var req = new XMLHttpRequest();'GET', url, false);  
      //XHR binary charset opt by Marcus Granado 2006 []  
      req.overrideMimeType('text/plain; charset=x-user-defined');  
      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 :

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
    Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.
    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.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now