Alternative to readAsBinaryString for IE10

2019-03-19 19:48发布

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

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

However, this results in an Out of stack space error.

4条回答
聊天终结者
2楼-- · 2019-03-19 20:23

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":

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);
    }
}
查看更多
老娘就宠你
3楼-- · 2019-03-19 20:24

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);
        }
    }
查看更多
闹够了就滚
4楼-- · 2019-03-19 20:40

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]);
}
查看更多
放我归山
5楼-- · 2019-03-19 20:46

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

  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);
  }
查看更多
登录 后发表回答