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
blob.arrayBuffer().then(buffer=>/* process the ArrayBuffer */);
222
-
223
222
```
224
223
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
224
## From Blob to stream
228
225
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`.
226
+
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.
230
227
231
-
```js
228
+
A stream is a special object that allows to read from it (or write into it) portion by portion. It's outside of our scope here, but here's an example, and you can read more at <https://developer.mozilla.org/en-US/docs/Web/API/Streams_API>.
229
+
230
+
The `Blob` interface's `stream()` method returns a `ReadableStream` which upon reading returns the data contained within the `Blob`.
232
231
232
+
Then we can read from it, like this:
233
+
234
+
```js
233
235
// get readableStream from blob
234
236
constreadableStream=blob.stream();
235
-
constreader=readableStream.getReader();
237
+
conststream=readableStream.getReader();
236
238
237
239
while (true) {
238
-
//value => blob fragments
239
-
let { done, value } =awaitreader.read();
240
+
//for each iteration: data is the next blob fragment
241
+
let { done, data } =awaitstream.read();
240
242
if (done) {
243
+
// no more data in the stream
241
244
console.log('write done.');
242
245
break;
243
246
}
244
-
// to do sth
245
-
stream.write(value);
247
+
248
+
// do something with the data portion we've just read from the blob
0 commit comments