【问题标题】:Javascript: Read raw bytes from XMLHttpRequest - messed upJavascript:从 XMLHttpRequest 读取原始字节 - 搞砸了
【发布时间】:2014-03-23 02:27:19
【问题描述】:

我正在使用这个原始问题中的代码 sn-p:Read bytes from a binary file with JavaScript, without jQuery

但是,由于某些原因,似乎加载了一组完全不同的字节!我怀疑这与字符串转换有关。

这是我要下载的二进制文件的副本:http://steeman.dk/html5/coco/colorbasic13.rom

在我的本地 IIS 7.5 服务器中,我已将 .rom MIME 类型添加为“application/octet-stream”(我也尝试过使用“text/plain; charset=x-user-defined”,结果相同)。

期待的是一个以此开头的字节序列:

a1 cb a2 82 a7 7c a7 0b a7 f4 a9 de a7 d8 10 ce (etc.) 

但是,我得到如下:

fd e2 fd fd 7c fd 0b fd fd fd a7 fd 10 fd 03 c6 37 fd fd 23 (etc.) 

除了很多'fd'被分散之外,我并没有真正看到一个清晰的模式。是什么赋予了?

顺便说一句,使用 JQuery 是否有更简单的方法?

【问题讨论】:

    标签: javascript jquery xmlhttprequest byte


    【解决方案1】:

    看起来有一种新方法,通过使用 Uint8Array 并将响应类型设置为“arraybuffer”:

    function loadRom(file, callback) 
    {
       var xhr = new XMLHttpRequest();
       xhr.open('GET', file, true);
       xhr.responseType = 'arraybuffer';
       xhr.onload = function (e) 
       {
        if (this.status == 200) 
        {
            var bytes = new Uint8Array(this.response);
            callback(bytes);
        }
      }
    xhr.send();
    }
    

    这对我有用!

    【讨论】:

      猜你喜欢
      • 2010-11-01
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      相关资源
      最近更新 更多