javascript IE10 的 readAsBinaryString 的替代方案

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18582643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:27:58  来源:igfitidea点击:

Alternative to readAsBinaryString for IE10

javascripthtmlinternet-explorer-10filereader

提问by David Jones

It seems that readAsBinaryString, a method of the JavaScript FileReaderobject, is not supported in IE10. I've tried the following, as suggested in this HTML5 Rocks article:

IE10似乎不支持JavaScript对象的一个方法readAsBinaryStringFileReader。我已经尝试了以下内容,如这篇 HTML5 Rocks 文章中所建议的:

String.fromCharCode.apply(null, new Uint16Array(buffer));

However, this results in an Out of stack spaceerror.

但是,这会导致Out of stack space错误。

回答by David Jones

I found the answer here:

我在这里找到了答案:

var binary = "";
var bytes = new Uint8Array(buffer);
var length = bytes.byteLength;
for (var i = 0; i < length; i++) {
  binary += String.fromCharCode(bytes[i]);
}

回答by John Reilly

If you'd like something a little terser and ES2015-ier then this may be what you're after:

如果你想要一些更简洁和 ES2015-ier 的东西,那么这可能就是你所追求的:

  const reader = new FileReader();
  reader.onerror = e => alert("File cannot be opened");

  if (reader.readAsBinaryString) {
     reader.onload = e => alert(e.target.result));
     reader.readAsBinaryString(file);
  }
  else {
     // Catering for IE 10/11
     reader.onload = e => {
        const bytes = new Uint8Array(e.target.result);
        const binary = bytes.reduce((acc, byte) => acc + String.fromCharCode(byte), "");
        alert(binary);
     };
     reader.readAsArrayBuffer(file);
  }

回答by fireb86

From the David Jones's answer, I wrote this method. The try/catch handles the readAsBinaryString exception in IE10/11 and call itself the "IE mode":

从大卫琼斯的回答中,我写了这个方法。try/catch 处理 IE10/11 中的 readAsBinaryString 异常并将自身称为“IE 模式”:

function readBinaryStringFromBlob(blob, callback, ie) {
    var reader = new FileReader();
    if(!ie) {
        reader.addEventListener("loadend", function () {
            callback(reader.result);
        });
        try {
            reader.readAsBinaryString(blob);
        } catch (err) {
            readBinaryStringFromBlob(blob, callback, true);
        }
    } else {
        reader.addEventListener("loadend", function () {
            var binary = "";
            var bytes = new Uint8Array(reader.result);
            var length = bytes.byteLength;
            for (var i = 0; i < length; i++) {
                binary += String.fromCharCode(bytes[i]);
            }
            callback(binary);
        });
        reader.readAsArrayBuffer(blob);
    }
}

回答by Alex Nikulin

Try my code:

试试我的代码:

    function readAsBinaryString(blob, callback) {
        var reader = new FileReader();

        var binStringCallback = function (e) {
            callback(e.target.result);
        };

        var arrBufferCallback = function (e) {
            var binary = "";
            var bytes = new Uint8Array(e.target.result);
            var length = bytes.byteLength;
            for (var i = 0; i < length; i++) {
                binary += String.fromCharCode(bytes[i]);
            }
            callback(binary);
        };

        reader.onerror = reader.onabort = function () {
            callback(null)
        };

        if (typeof reader.readAsBinaryString != "undefined") {
            reader.onload = binStringCallback;
            reader.readAsBinaryString(blob);
        } else {
            reader.onload = arrBufferCallback;
            reader.readAsArrayBuffer(blob);
        }
    }