Skip to content

Commit fb07094

Browse files
authored
Update blob article.md
Updated the latest methods for blob to arrayBuffer, blob to stream, and some tips for using them. https://developer.mozilla.org/en-US/docs/Web/API/Blob/arrayBuffer https://developer.mozilla.org/en-US/docs/Web/API/Blob/stream
1 parent f2e4db7 commit fb07094

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

4-binary/03-blob/article.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,21 +211,40 @@ For screenshotting a page, we can use a library such as <https://github.com/nikl
211211

212212
The `Blob` constructor allows to create a blob from almost anything, including any `BufferSource`.
213213

214-
But if we need to perform low-level processing, we can get the lowest-level `ArrayBuffer` from it using `FileReader`:
214+
But if we need to perform low-level processing, we can get the lowest-level `ArrayBuffer` from `blob.arrayBuffer()`:
215215

216216
```js
217217
// get arrayBuffer from blob
218-
let fileReader = new FileReader();
218+
const bufferPromise = await blob.arrayBuffer();
219219

220-
*!*
221-
fileReader.readAsArrayBuffer(blob);
222-
*/!*
220+
// or
221+
blob.arrayBuffer().then(buffer => /* process the ArrayBuffer */);
223222

224-
fileReader.onload = function(event) {
225-
let arrayBuffer = fileReader.result;
226-
};
227223
```
228224

225+
`FileReader.readAsArrayBuffer()` method can also get ArrayBuffer, but it usually fails if the blob memory exceeds 2G. It is recommended to use blob.arrayBuffer(), it's simpler.
226+
227+
## From Blob to stream
228+
229+
When we read and write to a blob of more than `2G`, the use of `arrayBuffer` becomes more memory intensive for us. At this point, we can directly convert the blob to a `stream`,The `Blob` interface's `stream()` method returns a `ReadableStream` which upon reading returns the data contained within the `Blob`.
230+
231+
```js
232+
233+
// get readableStream from blob
234+
const readableStream = blob.stream();
235+
const reader = readableStream.getReader();
236+
237+
while (true) {
238+
// value => blob fragments
239+
let { done, value } = await reader.read();
240+
if (done) {
241+
console.log('write done.');
242+
break;
243+
}
244+
// to do sth
245+
stream.write(value);
246+
}
247+
```
229248

230249
## Summary
231250

@@ -237,5 +256,7 @@ Methods that perform web-requests, such as [XMLHttpRequest](info:xmlhttprequest)
237256

238257
We can easily convert between `Blob` and low-level binary data types:
239258

240-
- We can make a Blob from a typed array using `new Blob(...)` constructor.
241-
- We can get back `ArrayBuffer` from a Blob using `FileReader`, and then create a view over it for low-level binary processing.
259+
- We can make a `Blob` from a typed array using `new Blob(...)` constructor.
260+
- We can get back `ArrayBuffer` from a Blob using `blob.arrayBuffer()`, and then create a view over it for low-level binary processing.
261+
262+
Conversion streams are very useful when we need to handle large blob. You can easily create a `ReadableStream` from a blob. The `Blob` interface's `stream()` method returns a `ReadableStream` which upon reading returns the data contained within the blob.

0 commit comments

Comments
 (0)