bianbian coding life

便便代码人生: 关注技术, 翻译文档, 偶尔动动手

[译] JavaScript (XMLHttpRequest) 读取二进制数据流

Posted by bianbian on 2008-04-06 13:36

本文Tags: , , , , ,

译者注:原来想用JS直接处理C丢过来的struct数据,具体可以查看:[原] C的struct和JSON交互
经过千辛万苦的查找,找到了这篇文章(作者应该比我更辛苦):Downloading Binary Streams with Javascript XMLHttpRequest
把关键点翻译如下(不过我还没有测试):
利用XMLHttpRequest的overrideMimeType方法设置charset为x-user-defined。

  1. //fetches BINARY FILES synchronously using XMLHttpRequest
  2. load_url = function(url) { 
  3.   netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  4.   var req = new XMLHttpRequest();
  5.   req.open('GET',url,false);
  6.   //XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
  7.   req.overrideMimeType('text/plain; charset=x-user-defined');
  8.   req.send(null);
  9.   if (req.status != 200) return '';
  10.   return req.responseText;
  11. }
  12.  
  13. var filestream = load_url(url);
  14. var abyte = filestream.charCodeAt(x) & 0xff;

IE不支持overrideMimeType方法,不过有评论者说VBScript可以实现:

  1. Dim xhr
  2. Set xhr = CreateObject("Microsoft.XMLHTTP")
  3. xhr.Open "GET", "folder.bin", False
  4. xhr.setRequestHeader "Accept-Charset", "x-user-defined"
  5. xhr.setRequestHeader "Content-Type", "application/pdf"
  6. xhr.send Null
标签: , , , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

(required)