weixin_33737774 2016-05-23 15:45 采纳率: 0%
浏览 36

检索二进制数据

Can anyone help me with this please?

I have a simple test Java servlet as shown below:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
         byte[] bytes = ReadWaveformAsBinary();

         response.setContentType("application/octet-stream");
         response.setContentLength(bytes.length);

         ServletOutputStream servletOutputStream = response.getOutputStream();

         servletOutputStream.write(bytes, 0, bytes.length);
         servletOutputStream.flush();
         servletOutputStream.close();
    }

This function works. It returns a byte array with 10 double precision numbers in it. I know its all working because I can call it from a C# application 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;
        }

The resultant byte array has the expected size of 80 bytes (10 * 8 bytes) and the 10 numbers are all as expected.

My question is, how can I call this Java servlet from JavaScript via an AJAX call?

For instance, I tried the following:

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

                    ajaxRequestObject.responseType = "arraybuffer";

                    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);
                }

But the alert box says 19 (not 80 as I hoped it would).

Thanks for any help.

As suggested I try the following but I get the same result :(

   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);

                                        OnCallCompleted("1,-1,0,0,-1,1");
                                    }
                                }
                            }
                        }*/

                        ajaxRequestObject.send(szParams);
                    }

}

I still see 19 and not 80.

  • 写回答

2条回答 默认 最新

  • 零零乙 2016-05-23 15:51
    关注

    You should use the "onload" event as in this example to get the complete payload/response.

    https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data

    评论

报告相同问题?

悬赏问题

  • ¥20 一个python博客项目的相关图例
  • ¥15 轮廓提取也提取不到,有没有别的方法,如何解决?
  • ¥50 Js和c++如何将含有图片的excel文件上传到后台服务器
  • ¥15 光电神经网络,FPGA
  • ¥20 通过防火墙出入站阻止游戏程序联网失效
  • ¥15 鼠标是可以在QT界面上移动的,但是热拔插鼠标无法移动了同时板子上是没问题的,如何解决?
  • ¥15 iframe嵌套显示问题
  • ¥20 【UE4】别人打包好的ue4游戏我该如何在自己的ue4引擎上运行
  • ¥15 power bi发布的链接地址打不开
  • ¥15 pip list列表中有库,但是编译时就显示缺少库