You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 4-binary/03-blob/article.md
+31-10Lines changed: 31 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -211,21 +211,40 @@ For screenshotting a page, we can use a library such as <https://github.com/nikl
211
211
212
212
The `Blob` constructor allows to create a blob from almost anything, including any `BufferSource`.
213
213
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()`:
215
215
216
216
```js
217
217
// get arrayBuffer from blob
218
-
let fileReader=newFileReader();
218
+
constbufferPromise=awaitblob.arrayBuffer();
219
219
220
-
*!*
221
-
fileReader.readAsArrayBuffer(blob);
222
-
*/!*
220
+
// or
221
+
blob.arrayBuffer().then(buffer=>/* process the ArrayBuffer */);
223
222
224
-
fileReader.onload=function(event) {
225
-
let arrayBuffer =fileReader.result;
226
-
};
227
223
```
228
224
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
+
constreadableStream=blob.stream();
235
+
constreader=readableStream.getReader();
236
+
237
+
while (true) {
238
+
// value => blob fragments
239
+
let { done, value } =awaitreader.read();
240
+
if (done) {
241
+
console.log('write done.');
242
+
break;
243
+
}
244
+
// to do sth
245
+
stream.write(value);
246
+
}
247
+
```
229
248
230
249
## Summary
231
250
@@ -237,5 +256,7 @@ Methods that perform web-requests, such as [XMLHttpRequest](info:xmlhttprequest)
237
256
238
257
We can easily convert between `Blob` and low-level binary data types:
239
258
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