简体   繁体   English

如何访问从Blob创建的ArrayBuffer的索引?

[英]How do I access the indices of an ArrayBuffer created from a Blob?

I do not consider myself a programmer, so I have found many, many answers to my questions by searching this site. 我不认为自己是程序员,因此通过搜索此网站,我发现了很多问题的答案。 I have been unable to figure this one out, though. 不过,我一直无法弄清楚这一点。

Using Javascript (really the only language I can even attempt right now), I am trying to upload a file and load its binary data into an ArrayBuffer for editing. 我试图使用Javascript(实际上是我现在甚至可以尝试的唯一语言),试图将文件上传并将其二进制数据加载到ArrayBuffer中进行编辑。 I am utilizing both FileSaver.js and Blob.js to increase compatibility. 我同时利用FileSaver.js和Blob.js来增加兼容性。 I have the user select the file, then load the file. 我让用户选择文件,然后加载文件。 When loading commences, a FileReader uses readAsArrayBuffer to load the file into an ArrayBuffer. 开始加载时,FileReader使用readAsArrayBuffer将文件加载到ArrayBuffer中。 I currently have it set to give an alert to check on the contents of the ArrayBuffer, but I will eventually remove that and simply work with the data. 我目前已将其设置为发出警报,以检查ArrayBuffer的内容,但最终我将删除它并仅使用数据。

If I take out the index, the alert knows that it is displaying an ArrayBuffer, but whenever I pick an index (like [0]), it tells me it's undefined. 如果我删除索引,警报会知道它正在显示ArrayBuffer,但是每当我选择一个索引(如[0])时,它就会告诉我它是未定义的。 How do I get the file data into an ArrayBuffer? 如何将文件数据放入ArrayBuffer? And how will it be divided up? 以及如何划分? Preferably I'd like 1 byte to an index. 最好我想给索引一个字节。

function loadButtonPressed() {
    var loadedFile = document.getElementById('loadFile').files;
    var testFile = loadedFile[0];
    var reader = new FileReader();
    reader.onloadend = function(evt) {
        if (evt.target.readyState == FileReader.DONE) {
            alert(String(reader.result[0]));
        }
    };
    reader.readAsArrayBuffer(testFile);
}

A buffer is just a series of bytes with no meaning. 缓冲区只是一系列无意义的字节。 JS provides no way to directly manipulate a buffer. JS没有提供直接操作缓冲区的方法。 To look into a buffer, you need a view, that defines how the buffer is interpreted - either a typed array, or a DataView object. 要查看缓冲区,您需要一个视图,该视图定义如何解释缓冲区-类型数组或DataView对象。 Try this: 尝试这个:

// ...
let buffer = reader.result;
let view = Int8Array(buffer);
console.log(view[0]);

or this: 或这个:

// ...
let buffer = reader.result;
console.log(new DataView(buffer).getInt8(0))

To be able to access an ArrayBuffer's data, you need to create a View from it. 为了能够访问ArrayBuffer的数据,您需要从中创建一个View。

This can be done in two ways: 这可以通过两种方式完成:

TypedArrays TypedArrays
These are array like objects, each byte of your ArrayBuffer will be represented in the chosen TypedArray type. 这些是类似数组的对象,ArrayBuffer的每个字节将以所选的TypedArray类型表示。 Note that this will use the system's endianness. 请注意,这将使用系统的字节序。

 var buf = new TextEncoder().encode('Hello world').buffer; console.log(buf); // create an Uint8 view var view = new Uint8Array(buf); console.log(view); // now you can iterate and modify your arrayBuffer from this view. view[0] = 23; console.log(new Uint8Array(buf)[0]); 

DataView . DataView
This is slower but gets more options to get and set data in your ArrayBuffer. 这比较慢,但是有更多选项来获取和设置ArrayBuffer中的数据。 Eg, you can set the endianness, you don't need to check the byteLength of your buffer to get Int32 values etc. 例如,您可以设置字节序,无需检查缓冲区的byteLength即可获取Int32值等。

 var buf = new TextEncoder().encode('Hello world').buffer; console.log(buf); // create a dataView var view = new DataView(buf); console.log(view.getUint8(0)); // now you can iterate and modify your arrayBuffer from this view. view.setUint8(0, 23); console.log(new Uint8Array(buf)[0]); 

Note: Both methods will directly modify the ArrayBuffer's data, and its data will not be cloned. 注意:两种方法都将直接修改ArrayBuffer的数据,并且不会克隆其数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Blob 到 ArrayBuffer - How to go from Blob to ArrayBuffer 如何从 Formidable 解析的 object 中提取 Blob 和 ArrayBuffer / TypedArray? Node.js - How do I extract the Blob and the ArrayBuffer / TypedArray from the Formidable parsed object? In Node.js 从arraybuffer到Cordova中的Blob - From arraybuffer to blob in Cordova 如何访问 Blob 的值? - How do I access a value of a Blob? Javascript - 我从一个字符串创建了一个 blob,如何取回字符串? - Javascript - I created a blob from a string, how do I get the string back out? 如何使ArrayBuffer在Javascript中不可变? - How do I make an ArrayBuffer immutable in Javascript? 如何从后面的代码访问这些动态创建的控件? - How do I access these dynamically created controls from the code behind? 从Blob读取ArrayBuffer的数据错误 - Reading ArrayBuffer from Blob has wrong data Chrome扩展程序:如何将ArrayBuffer或Blob从内容脚本传递到后台而不会丢失其类型? - Chrome extension: how to pass ArrayBuffer or Blob from content script to the background without losing its type? 如何在没有 FileReader 的情况下从 Blob 和 File 对象创建 ArrayBuffer 和数据 URI? - How to create an ArrayBuffer and data URI from Blob and File objects without FileReader?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM