Javascript - 从 ArrayBuffer 中获取数据?

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

Javascript - get data out of ArrayBuffer?

javascriptdrag-and-droparraybuffer

提问by Ben

I've got a drag and drop script that uses readAsArrayBuffer(). The length of the buffer is perfect, but I can't seem to figure out how to pull the data out of the buffer.

我有一个使用readAsArrayBuffer(). 缓冲区的长度是完美的,但我似乎无法弄清楚如何将数据拉出缓冲区。

Apparently I've got to make a DataViewor an Uint8Arrayor something, then iterate through its byteLength...help!

显然我必须制作一个DataView或一个Uint8Array或其他东西,然后遍历它的byteLength...帮助!

EDITPertinent code (there's not much of it):

编辑相关代码(内容不多):

var reader = new FileReader();
reader.onload = function(e) {
    // do something with e.target.result, which is an ArrayBuffer
} 
reader.readAsArrayBuffer(someFileHandle);

采纳答案by haylem

This might change based on your answer to my comment, but if I assume that you are using a FileReadersomewhere, you need to read it's resultattribute in the loadedcallback that you need to provide:

这可能会根据您对我的评论的回答而改变,但是如果我假设您正在使用FileReader某个地方,则需要resultloaded您需要提供的回调中读取它的属性:

function loaded(evt) {  
  var datastring = evt.target.result;

  // do something here
}

reader.onload = loaded; // where reader is a FileReader, FileReaderSync 

Update:Ah, I see. Well then your best course of action is to follow to this duplicate:

更新:啊,我明白了。那么你最好的行动方案是遵循这个副本:

Converting between strings and ArrayBuffers

在字符串和 ArrayBuffers 之间转换

Update2:Note that you could probably use readAsText()then, but I don't know if you're at liberty to do this.

更新 2:请注意,您可能会在readAsText()那时使用,但我不知道您是否可以自由执行此操作。