2012-05-24 109 views
5

大量尝试读取将代表jpg图像的二进制数据流httprequest的响应之后,仍然挣扎着。javascript中的httprequest和二进制数据

编辑:整个事情

xmlhttp = false; 
     /*@[email protected]*/ 
     /*@if (@_jscript_version >= 5) 
     // JScript gives us Conditional compilation, we can cope with old IE versions. 
     // and security blocked creation of the objects. 
     try { 
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
      try { 
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (E) { 
       xmlhttp = false; 
      } 
     } 
     @[email protected]*/ 
     if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { 
      try { 
       xmlhttp = new XMLHttpRequest(); 
      } catch (e) { 
       xmlhttp = false; 
      } 
     } 
     if (!xmlhttp && window.createRequest) { 
      try { 
       xmlhttp = window.createRequest(); 
      } catch (e) { 
       xmlhttp = false; 
      } 
     } 

     xmlhttp.open("GET", theUrl, true); 
     xmlhttp.onreadystatechange = function() { 
      if (xmlhttp.readyState == 4) { 

       var headers = xmlhttp.getAllResponseHeaders(); 
      } 
     } 

     xmlhttp.send(null); 

我使用IE8,并没有HTML 5(也试过在FF12) 所以我总是用一些错误落得像

xhr.overrideMimeType('text\/plain; charset=x-user-defined'); 

xhr.responseType = "arraybuffer"; 

即使复制到一个变量不会工作

var body = xhr.response; //.responseText , .responseBody 

任何想法最新错误或wwhat我可以尝试?

+0

为什么?你想达到什么目的?你的服务器可以做b64编码吗? – mplungjan

+0

没有它的嵌入式设备和公司不会悲伤地改变他们的界面 – Gobliins

+0

但是,你应该如何在脚本端使用二进制数据? – Raffaele

回答

1

我使this example在客户端读取二进制JPEG并解析EXIF数据。它使用BinFileReader.js,这使得处理二进制数据,跨浏览器非常容易。 Here is the whole thing in a zip file,包括一个基于节点的网络服务器(与节点server.js一起运行)我还包括一个web-worker example

如果您只是从二进制流中制作图像,为什么不只是将<img src="blah" />添加到您的DOM?

$('body').append('<img src="' + URL + '"/>'); 
+0

这是否回答您的问题? – konsumer