readAsBinaryString api:chrome
https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader/readAsBinaryStringapi
注意:瀏覽器
該特性是非標準的,請儘可能不要在生產環境中使用它!app
通過測試:ie11瀏覽器不支持,chrome等是支持的。測試
解決方案1:this
使用: readAsArrayBuffer 而後: function arrayBufferToString(buffer) { var array = new Uint8Array(buffer); return binaryToString(String.fromCharCode.apply(null, Array.prototype.slice.apply(array))); }
該訪問問題:若是buffer比較大,那麼ie11可能出現堆棧溢出。spa
解決方案2:自定義prototype
readAsBinaryString
if (r.readAsBinaryString === undefined) { FileReader.prototype.readAsBinaryString = function (fileData) { //解決ie11 大文件堆棧溢出的問題(for arrayBufferToString) var binary = ""; var pt = this; var reader = new FileReader(); reader.onload = function (e) { var bytes = new Uint8Array(reader.result); var length = bytes.byteLength; for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); } pt.content = binary; //console.log("binary length:" + binary.length); pt.onload(pt); //頁面內data取pt.content文件內容 } reader.readAsArrayBuffer(fileData); } }