javascript ArrayBuffer 和 Blob 有什么区别?

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

What is the difference between an ArrayBuffer and a Blob?

javascripthtml

提问by dangerChihuahua007

I'm reading http://www.html5rocks.com/en/tutorials/file/xhr2/and trying to figure out the difference between an ArrayBuffer and a Blob.

我正在阅读http://www.html5rocks.com/en/tutorials/file/xhr2/并试图找出 ArrayBuffer 和 Blob 之间的区别。

Aren't both containers comprised of bits? Hence, couldn't both containers be viewed in many ways (as 32-bit chunks, 16-bit chunks, etc.)?

两个容器不是都由位组成吗?因此,不能以多种方式查看两个容器(如 32 位块、16 位块等)?

采纳答案by Halcyon

It's explained on the page.

页面上有说明。

ArrayBuffer

数组缓冲区

An ArrayBuffer is a generic fixed-length container for binary data. They are super handy if you need a generalized buffer of raw data, but the real power behind these guys is that you can create "views" of the underlying data using JavaScript typed arrays. In fact, multiple views can be created from a single ArrayBuffer source. For example, you could create an 8-bit integer array that shares the same ArrayBuffer as an existing 32-bit integer array from the same data. The underlying data remains the same, we just create different representations of it.

ArrayBuffer 是二进制数据的通用固定长度容器。如果您需要原始数据的通用缓冲区,它们非常方便,但这些家伙背后的真正力量是您可以使用 JavaScript 类型化数组创建底层数据的“视图”。事实上,可以从单个 ArrayBuffer 源创建多个视图。例如,您可以创建一个 8 位整数数组,该数组与来自相同数据的现有 32 位整数数组共享相同的 ArrayBuffer。底层数据保持不变,我们只是创建了它的不同表示。

BLOB

BLOB

If you want to work directly with a Blob and/or don't need to manipulate any of the file's bytes, use xhr.responseType='blob':

如果您想直接使用 Blob 和/或不需要操作任何文件的字节,请使用 xhr.responseType='blob':

回答by The Red Pea

Summary

概括

Unless you need the ability to write/edit(using an ArrayBuffer), then Blobformat is probably best.

除非您需要编写/编辑的能力(使用ArrayBuffer),否则Blob格式可能是最好的。

Detail

细节

I came to this question from a differenthtml5rocks page., and I found @Bart van Heukelom's commentsto be helpful, so I wanted to elevate them to an answer here.

我是从另一个html5rocks 页面提出这个问题的,我发现@Bart van Heukelom 的评论很有帮助,所以我想在这里将它们提升为答案。

I also found it helpful to find resources specific to ArrayBufferand Blobobjects. I added the emphasis to reiterate the helpful detail I was looking for. In summary: despite the emphasis on Blobbeing "raw data" it's very workable.

我还发现查找特定于ArrayBufferBlob对象的资源很有帮助。我添加了重点以重申我正在寻找的有用细节。总之:尽管强调Blob是“原始数据”,但它非常可行

Some other points on ArrayBuffervs Blob:

ArrayBuffervs 的其他一些要点Blob

Here are the documentation details that helped me:

以下是对我有帮助的文档详细信息:

Here is ArrayBuffer

这是 ArrayBuffer

The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. You cannot directlymanipulate the contents of an ArrayBuffer; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and writethe contents of the buffer.

ArrayBuffer 对象用于表示通用的、固定长度的原始二进制数据缓冲区。您不能直接操作 ArrayBuffer 的内容;相反,您创建一个类型化数组对象或一个以特定格式表示缓冲区的 DataView 对象,并使用它来读取和写入缓冲区的内容。

Here is Blob

这是 Blob

A Blob object represents a file-like object of immutable, raw data. Blobs represent data that isn't necessarily in a JavaScript-native format. The File interfaceis based on Blob, inheriting blob functionality and expanding it to support files on the user's system.

Blob 对象表示不可变的原始数据的类文件对象。Blob 表示不一定采用 JavaScript 原生格式的数据。File接口基于 Blob,继承 Blob 功能并对其进行扩展以支持用户系统上的文件。