【问题标题】:Read binary in IE9 with jQuery and jDataView使用 jQuery 和 jDataView 在 IE9 中读取二进制文件
【发布时间】:2013-05-31 08:28:41
【问题描述】:

我正在尝试在 2 个浏览器上使用 jDataView 读取二进制文件:Chrome 和 IE9。

我在 ajax 请求之前添加了新类型 binary(使用 jQuery 1.10.0):

// Install binary dataType
jQuery.ajaxSetup({
    accepts: {
        binary: "text/plain; charset=x-user-defined"
    },
    contents: {

    },
    converters: {
        "text binary": true // Nothing to convert
    }
});

对于返回二进制流的服务器,我添加了标头:

<?php header("Content-type: text/html; charset=windows-1251"); ?>

然后是ajax请求:

$.support.cors = true;
$.ajax({
    type: 'GET',
    url: url,
    dataType: 'binary',
    mimeType: 'text/plain; charset=x-user-defined',
    success: function(data) {
        var view = new jDataView(data);
        // ...
    }
});

我正在使用方法getUint8() 来获取二进制流的一部分:

for (var l = 0; l < 8; l++) {
    tx += " " + view.getUint8(l, true);
}

然后,比较tx 字符串:

0 0 0 7 12 106 212 65 (chrome) => GOOD (match the expected results)
0 0 2 94 12 106 36 65 (IE9) => 3 BAD sequences

在 Chrome 上它工作得很好,但在 IE9 上我没有相同的结果...... Chrome 使用本机 getUint8 函数,而 IE9 使用 jDataView 方法。

【问题讨论】:

  • 检查两件事:1)您的for 循环在每次迭代中具有相同的l 值。如果是这样,那么有问题的代码不足以找出原因; 2)当您从两个浏览器获得服务器响应时,它是相等的。如果是这样,jDataView的getUint8方法有问题; github 在问题选项卡上有类似 bugtracker 的东西,在那里报告。

标签: javascript jquery binary internet-explorer-9


【解决方案1】:

最后我找到了这个hack 和 jDataView 的解决方案:

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

if (oReq.overrideMimeType) {
    oReq.overrideMimeType('text/plain; charset=x-user-defined');
} else {
    oReq.setRequestHeader('Accept-Charset', 'x-user-defined');
}

oReq.onload = function(oEvent) {
    if (oReq.status == 200) {
        var arrayBuffer = oReq.response;
        var view = new Uint8Array(arrayBuffer);
        var dataView = new jDataView(view.buffer);
        // ...
    }
};

【讨论】:

    猜你喜欢
    • 2012-08-06
    • 2011-11-22
    • 2021-12-04
    • 2019-04-13
    • 2020-11-11
    • 2012-09-11
    • 2019-09-25
    • 2017-06-28
    • 1970-01-01
    相关资源
    最近更新 更多