javascript FileReader.readAsArrayBuffer 返回为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11090659/
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-26 12:04:16  来源:igfitidea点击:

FileReader.readAsArrayBuffer return is null

javascripthtmlfileapi

提问by wonglik

I am following Html5rocks tutorial

我正在关注 Html5rocks 教程

http://www.html5rocks.com/en/tutorials/file/dndfiles/

http://www.html5rocks.com/en/tutorials/file/dndfiles/

and I am trying to use readAsArrayBuffer instead of readAsBinaryString in slicing a file example (because I want to read gif header to find file resolution). But I am stuck because evt.target.result (while using readAsBinaryString it is a string)

我试图在切片文件示例中使用 readAsArrayBuffer 而不是 readAsBinaryString (因为我想读取 gif 标头以查找文件分辨率)。但我被卡住了,因为 evt.target.result (虽然使用 readAsBinaryString 它是一个字符串)

any ideas?

有任何想法吗?

EDIT : Code

编辑:代码

reader.onloadend = function(evt) 

{

  if (evt.target.readyState == FileReader.DONE) { // DONE == 2
    document.getElementById('byte_content').textContent = evt.target.result;
    console.log(evt.target.result.byteLenght)
    document.getElementById('byte_range').textContent = 
        ['Read bytes: ', start + 1, ' - ', stop + 1,
         ' of ', file.size, ' byte file'].join('');
  }
};

if (file.webkitSlice) {
  var blob = file.webkitSlice(start, stop + 1);
} else if (file.mozSlice) {
  var blob = file.mozSlice(start, stop + 1);
}
reader.readAsArrayBuffer(blob);
}

so in Firefox 13 I get on screen : [object ArrayBuffer]

所以在 Firefox 13 中,我出现在屏幕上:[object ArrayBuffer]

and on console log : undefined

并在控制台日志上:未定义

while in Chromium 18 I get on console : Uncaught TypeError: Cannot read property 'byteLenght' of null

在 Chromium 18 中,我进入控制台:未捕获的类型错误:无法读取 null 的属性“byteLenght”

回答by colxi

There is a typing mistake in:

有一个打字错误:

console.log(evt.target.result.byteLenght)

byteLenghtshould be byteLength

byteLenght应该 byteLength