Just as a Blob is an opaque representation of data available on disk, an ArrayBuffer is an opaque representation of bytes available in memory.
就像Blob是磁盘上可用数据的不透明表示一样, ArrayBuffer是内存中可用字节的不透明表示。
The constructor takes one parameter, the length in bytes:
构造函数采用一个参数,长度以字节为单位:
const buffer = new ArrayBuffer(64)
An ArrayBuffer value has one (read-only) property: byteLength
, which - as the name suggests - expresses its length in bytes.
ArrayBuffer值具有一个(只读)属性: byteLength
,顾名思义,以字节为单位表示其长度。
It also provides a slice()
instance method which creates a new ArrayBuffer
from an existing one, taking a starting position and an optional length:
它还提供了slice()
实例方法,该方法从一个现有的实例创建一个新的ArrayBuffer
,并采用一个起始位置和一个可选的长度:
const buffer = new ArrayBuffer(64)
const newBuffer = buffer.slice(32, 8)
从互联网下载数据作为ArrayBuffer (Downloading data from the internet as an ArrayBuffer)
We can download a blob from the internet and store it into an ArrayBuffer using XHR:
我们可以从网上下载一个Blob,然后使用XHR将其存储到ArrayBuffer中:
const downloadBlob = (url, callback) => {
const xhr = new XMLHttpRequest()
xhr.open('GET', url)
xhr.responseType = 'arraybuffer'
xhr.onload = () => {
callback(xhr.response)
}
xhr.send(null)
}