如何使用AJAX检索二进制数据 [英] How to retrieve binary data with AJAX

查看:63
本文介绍了如何使用AJAX检索二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





任何人都可以通过以下方式帮助我吗?



我有一个非常简单的测试Java servlet如下:



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





工作正常。我知道这是因为我从C#应用程序中调用它如下:





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





工作正常。我按预期获得了80个字节的数据(8 * 10个双倍),数字是应该的。



我的问题是,如何使用AJAX调用它来自javascript?



我有以下内容:





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

}





如果我使用未注释的代码或评论无关紧要码。警报的结果是19,我期望80.我做错了什么?



最后,如果我得到它的工作,我怎么能用javascript将bytearray转换为双打? (漂浮?)。



感谢您的帮助,

Mitch。



我尝试过:



如上图所示。我已经从c#测试了servlet,并尝试了两种不同的方法来在JavaScript中调用它。两个都给了我一个byteArray.byteLength os 19而不是80.



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.

推荐答案

new Ajax.Request('/ viewImage?id = 123',{

//请求返回二进制图像输入流

onSuccess:function(transport){

//文本示例

// alert(transport) .responseText)



//问题:是否有流二进制响应?
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){

//处理失败

}

});
('imgElem').src = transport.responseBinary;
},
onFailure: function(transport) {
// handle failure
}
});


这篇关于如何使用AJAX检索二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆