阿贾克斯二元响应 [英] Ajax Binary Response

查看:266
本文介绍了阿贾克斯二元响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想知道是否有无论如何要流在AJAX二元响应?这将是一个最终的解决方案,否则我需要实现二进制映像文件,然后流该文件的用户提供不同的URL。

 新的Ajax.Request('/ viewImage?ID = 123',{
  //请求返回一个二进制图像的InputStream
  的onSuccess:函数(运输){
      //文字的例子
      //警报(transport.responseText)

      //问:有没有流二元响应?
      $('imgElem')SRC = transport.responseBinary。
  },
  onFailure:函数(运输){
      //处理失败
  }
});
 

解决方案

这可能无法为的二进制数据,但是可以使用Ajax来检索二进制数据。

这是有可能使用以下两种方法之一:使用Javascript 类型数组或XMLHtt presponse overrideMimeType 黑客。对MDN一篇好文章的读 - 这些例子都是从那里取:发送和接收二进制数据

该类型数组的方法是这样的:

  VAR oReq =新XMLHtt prequest();
oReq.open(GET,/myfile.png,真正的);
oReq.responseType =arraybuffer;

oReq.onload =功能(oEvent){
  VAR arrayBuffer = oReq.response; //注:不oReq.responseText
  如果(arrayBuffer){
    VAR的字节数组=新Uint8Array(arrayBuffer);
    对于(VAR I = 0; I< byteArray.byteLength;我++){
      //做一些与阵列中的每个字节
    }
  }
};

oReq.send(空);
 

类型数组中不支持IE< 10,火狐< 4,浏览器< 7,Safari浏览器< 5.1和Opera< 11.6,和移动的支持是不可靠的,但提高

第二种方法是使用所谓的overrideMimeType的XMLHtt prequest方法允许二进制数据原封不动的通过。

  VAR REQ =新XMLHtt prequest();
req.open(GET,/myfile.png',假);
// XHR二进制字符集选择由马库斯·格兰纳2006年[http://mgran.blogspot.com]
req.overrideMimeType(文\ /纯;字符集= X用户自定义);
req.send(空);
如果(req.status = 200!)返回'';
//做的东西跟req.responseText;
 

您获得一个未解析二进制字符串,在其中您可以使用 VAR字节= filestream.char $ C $猫(X)及为0xFF; 来检索特定字节

Hi I'm wondering if there's anyway to stream a binary response in AJAX? This would be an ultimate solution otherwise I would need to realize the binary image to a file then stream that file to the user with a different URL.

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

解决方案

It might not be possible to stream binary data, but you can use Ajax to retrieve binary data.

This is possible using one of two methods: Javascript Typed Arrays or an XMLHttpResponse overrideMimeType hack. Have a read of a good article on MDN – these examples are taken from there: Sending and Receiving Binary Data

The Typed Array method looks like this:

var oReq = new XMLHttpRequest();
oReq.open("GET", "/myfile.png", true);
oReq.responseType = "arraybuffer";

oReq.onload = function (oEvent) {
  var arrayBuffer = oReq.response; // Note: not oReq.responseText
  if (arrayBuffer) {
    var byteArray = new Uint8Array(arrayBuffer);
    for (var i = 0; i < byteArray.byteLength; i++) {
      // do something with each byte in the array
    }
  }
};

oReq.send(null);

Typed Arrays are not supported in IE < 10, Firefox < 4, Chrome < 7, Safari < 5.1 and Opera < 11.6, and mobile support is shaky but improving.

The second method uses an XMLHttpRequest method called overrideMimeType to allow the binary data to be passed through unmodified.

var req = new XMLHttpRequest();
req.open('GET', '/myfile.png', false);
// XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
req.overrideMimeType('text\/plain; charset=x-user-defined');
req.send(null);
if (req.status != 200) return '';
// do stuff with req.responseText;

You get an unparsed binary string, upon which you can use var byte = filestream.charCodeAt(x) & 0xff; to retrieve a specific byte.

这篇关于阿贾克斯二元响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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