XMLHttp java服务端与 js客户端通讯实现二进制流收发
2016-06-03 10:54
253 查看
XMLHttp java服务端与 js客户端通讯实现二进制流收发
js客户端:
var xhr = cc.loader.getXMLHttpRequest();
xhr.responseType = "arraybuffer";
var that = this;
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
var binary = new Uint8Array(xhr.response);
}
}
xhr.open("POST", "http://192.168.199.88:8787");
var str = "test=1&idLogin=10086";
var buf = new ArrayBuffer(str.length); // 1 bytes for each char
var view = new Uint8Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
view[i] = str.charCodeAt(i);
}
xhr.send(buf);
参考如下:http://blog.csdn.net/u011462674/article/details/9748359
利用XMLHttpRequest同步和异步下载二进制文件的解决方案。
在XMLHttpRequest里支持二进制数据的下载了,现分别以同步和异步两种方式分别介绍。
异步的方式下载:
[html]
view plain
copy
[javascript]
view plain
copy
xmlRequest.open("GET", "0.jpg", true);
xmlRequest.responseType = "blob";//这里是关键,它指明返回的数据的类型是二进制
xmlRequest.onreadystatechange = function(e) {
if (this.readyState == 4 && this.status == 200) {
var response = this.response;
img.src = window.URL.createObjectURL(response);
}
}
xmlRequest.send(null);
同步的方式下载:
[html]
view plain
copy
<pre name="code" class="javascript"> xmlRequest.open("GET", "0.jpg", false);
xmlRequest.overrideMimeType('text/plain; charset=x-user-defined');//这里是关键,不然 this.responseText;的长度不等于文件的长度
xmlRequest.onreadystatechange = function(e) {
if (this.readyState == 4 && this.status == 200) {
var text = this.responseText;
var length = text.length;
var array = new Uint8Array(length);
for (var i = 0; i < length; ++i) {
array[i] = text.charCodeAt(i);
}
var blob = new Blob([array], { "type": "image/jpeg" });
img.src = window.URL.createObjectURL(blob);
}
}
xmlRequest.send(null);</pre><br>
<pre></pre>
注意:w3c标准中规定同步的情况下是不能设置responseType属性的。
<p></p>
<p></p>
<p></p>
<pre></pre>
<pre></pre>
<pre></pre>
js客户端:
var xhr = cc.loader.getXMLHttpRequest();
xhr.responseType = "arraybuffer";
var that = this;
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
var binary = new Uint8Array(xhr.response);
}
}
xhr.open("POST", "http://192.168.199.88:8787");
var str = "test=1&idLogin=10086";
var buf = new ArrayBuffer(str.length); // 1 bytes for each char
var view = new Uint8Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
view[i] = str.charCodeAt(i);
}
xhr.send(buf);
参考如下:http://blog.csdn.net/u011462674/article/details/9748359
利用XMLHttpRequest同步和异步下载二进制文件的解决方案。
在XMLHttpRequest里支持二进制数据的下载了,现分别以同步和异步两种方式分别介绍。
异步的方式下载:
[html]
view plain
copy
[javascript]
view plain
copy
xmlRequest.open("GET", "0.jpg", true);
xmlRequest.responseType = "blob";//这里是关键,它指明返回的数据的类型是二进制
xmlRequest.onreadystatechange = function(e) {
if (this.readyState == 4 && this.status == 200) {
var response = this.response;
img.src = window.URL.createObjectURL(response);
}
}
xmlRequest.send(null);
同步的方式下载:
[html]
view plain
copy
<pre name="code" class="javascript"> xmlRequest.open("GET", "0.jpg", false);
xmlRequest.overrideMimeType('text/plain; charset=x-user-defined');//这里是关键,不然 this.responseText;的长度不等于文件的长度
xmlRequest.onreadystatechange = function(e) {
if (this.readyState == 4 && this.status == 200) {
var text = this.responseText;
var length = text.length;
var array = new Uint8Array(length);
for (var i = 0; i < length; ++i) {
array[i] = text.charCodeAt(i);
}
var blob = new Blob([array], { "type": "image/jpeg" });
img.src = window.URL.createObjectURL(blob);
}
}
xmlRequest.send(null);</pre><br>
<pre></pre>
注意:w3c标准中规定同步的情况下是不能设置responseType属性的。
<p></p>
<p></p>
<p></p>
<pre></pre>
<pre></pre>
<pre></pre>
相关文章推荐
- TCP滑动窗口机制
- URL与 HTTP,iOS中发送HTTP请求
- fatal: unable to access 'https://github.com/...': Could not resolve host: github.com
- 【网络编程】——windows socket 编程
- TCP/IP Modbus消息体格式
- 安装Apache后查看httpd状态出现“httpd dead but subsys locked”问题
- http的get方式连接服务器,获取数据
- poj 2112(二分+网络流)
- 用异步获取网络数据Json
- HTTP协议
- TCP三次握手,4次挥手
- java 通过http post方式传参参数json 方式
- Linux_ 网络配置及操作
- JavaWeb:报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- HTTP协议头及错误码详解
- HTTP和HTTPS的区别
- Android学习笔记六十四:浅谈TCP/IP协议栈(二)IP地址
- Android学习笔记六十三:浅谈TCP/IP协议栈(一)入门知识
- TcpFlow(1.4.6 C++版本)源代码分析
- 理解 LSTM 网络