Click here to Skip to main content
14,303,339 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

Can anyone help me with the following?

I have a very simple test Java servlet as follows:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
        byte[] bytes = GetNumbers1to10AsBinaryDataFromAFile() // My function to get bytes of {1,2,3,4,5,6,7,8,9,10} written as doubles to a binary file.
		        
		        response.setContentType("application/octet-stream");
		        response.setContentLength(bytes.length);
				
				ServletOutputStream servletOutputStream = response.getOutputStream();
				
				servletOutputStream.write(bytes, 0, bytes.length);
			    servletOutputStream.flush();
			    servletOutputStream.close();
}


It works fine. I know this because I call it from a C# app as below:

public static bool CallWebServiceDownloadEndPoint(string szWebEndPoint, string szRequest, out double[] data)
        {
            data = null;

            bool bSuccess = true;

            WebClient webClient = new WebClient();

            try
            {
                byte[] byteData = webClient.DownloadData(szWebEndPoint + "?" + szRequest);
                Array.Reverse(byteData);

                data = CreateDoubleArrayFromByteArray(byteData);
                Array.Reverse(data);
            }
            catch
            {
                bSuccess = false;
            }

            return bSuccess;
        }


Works fine. I get 80 bytes of data as expected (8 * 10 doubles) and the numbers are as they should be.

My problem is, how can I call this using AJAX from javascript?

I have the following:

function AJAXSendString2(ajaxRequestObject, szURL, szParams, OnCallCompleted)
    {
    if (ajaxRequestObject != null)
                    {
                        ajaxRequestObject.open("GET", szURL, true);

                        ajaxRequestObject.responseType = "arraybuffer";

                        ajaxRequestObject.onload = function(oEvent)
                        {
                            var arrayBuffer = ajaxRequestObject.response;

                            if(arrayBuffer)
                            {
                                var byteArray = new Uint8Array(arrayBuffer);

                                alert(byteArray.byteLength);
                            }
                        }

                        /*ajaxRequestObject.onreadystatechange = function ()
                        {
                            if (ajaxRequestObject.readyState == 4)
                            {
                                if (ajaxRequestObject.status == 200)
                                {
                                    var arrayBuffer = ajaxRequestObject.response;

                                    if(arrayBuffer)
                                    {
                                        var byteArray = new Uint8Array(arrayBuffer);

                                        alert(byteArray.byteLength);
                                    }
                                }
                            }
                        }*/

                        ajaxRequestObject.send(szParams);
                    }

}


Doesn't matter if I use the uncommented code or the commented code. The result of the alert is 19 and I expect 80. Am I doing something wrong?

Finally if I ever get it working how could I use javascript to convert bytearray to doubles? (floats?).

Thanks for any help,
Mitch.

What I have tried:

As shown above. I've tested the servlet from c# and tried two different ways to call it in JavaScript. Both give me a byteArray.byteLength os 19 not 80.
Posted
Updated 26-Jun-16 18:59pm
Comments
ZurdoDev 24-May-16 12:43pm
   
What will you do with byte data in JavaScript?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

new Ajax.Request('/viewImage?id=123', {
// request returns a binary image inputstream
onSuccess: function(transport) {
// text example
// alert(transport.responseText)

// QUESTION: is there a streaming binary response?
$('imgElem').src = transport.responseBinary;
},
onFailure: function(transport) {
// handle failure
}
});
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100