- From: guest271314 <notifications@github.com>
- Date: Sun, 05 Apr 2020 13:26:11 -0700
- To: w3c/FileAPI <FileAPI@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <w3c/FileAPI/issues/150/609477046@github.com>
The adjacent, or opposite, case is passing an array of `TypedArray`s, to `Blob()` with `[]` included, e.g., `new Blob([[Uint8Array, Uint8Array, ..., Uint8Array]])` where when the data is attempted to be converted back to original values, the result will not be expected. In brief,
```
Promise.all(promises)
.then(result => {
console.log(result); // (890) [Uint8Array(36), Uint8Array(38), ... Uint8Array(38)]
/*
0: Uint8Array(36) [82, 73, 70, 70, 28, 0, 0, 0, 87, 69, 66, 80, 86, 80, 56, 76, 16, 0, 0, 0, 47, 149, 64, 37, 0, 7, 80, 194, 168, 66, 255, 3, 17, 209, 255, 0]
...
*/
let blob, file;
// RangeError: Invalid string length
// Array.join (<anonymous>)
// Array.toString (<anonymous>)
try {
blob = new Blob([result]);
console.log(blob);
blob.arrayBuffer().then(buffer => {
console.log(new Uint8Array(buffer)); // Uint8Array(94848) [56, 50, 44, 55, 51, 44, 55, 48, 44, 55, 48, 44, 50, 56, 44, 48, 44, 48, 44, 48, 44, 56, 55
})
} catch (e) {
console.error(e);
}
});
```
Now, if we try we will get an error
```
createImageBitmap(new Blob([new Uint8Array([56, 50, 44, 55, 51, 44, 55, 48, 44, 55, 48, 44, 50, 56, 44, 48, 44, 48, 44, 48, 44, 56, 55, 44, 54, 57, 44, 54, 54, 44, 56, 48, 44, 56, 54, 44])]))
.then(console.log).catch(console.error);
Promise {<pending>}
663a7444cff629a41b76.js:109 DOMException: The source image could not be decoded.
```
where if we got back the same results we will get
```
createImageBitmap(new Blob([new Uint8Array([82, 73, 70, 70, 28, 0, 0, 0, 87, 69, 66, 80, 86, 80, 56, 76, 16, 0, 0, 0, 47, 149, 64, 37, 0, 7, 80, 194, 168, 66, 255, 3, 17, 209, 255, 0])]))
.then(console.log);
Promise {<pending>}
663a7444cff629a41b76.js:109 ImageBitmap {width: 150, height: 150}
```
What happened to the original input data? Simply using `[]` directly affected the result when converting back to original data?
Evidently, yes. In this case, potentially relatively common, the resulting promise value from `Promise.all()`, we do not need to use `[]`
```
Promise.all(promises)
.then(result => {
console.log(result); // (892) [Uint8Array(36), Uint8Array(36), Uint8Array(38)...]
/*
0: Uint8Array(36) [82, 73, 70, 70, 28, 0, 0, 0, 87, 69, 66, 80, 86, 80, 56, 76, 16, 0, 0, ...]
*/
let blob, file;
// RangeError: Invalid string length
// Array.join (<anonymous>)
// Array.toString (<anonymous>)
try {
blob = new Blob(result);
console.log(blob); // Blob {size: 33164, type: ""}
blob.arrayBuffer().then(buffer => {
// now we can use subarray(), slice(), ReadableStream/WritableStream, etc. to re-create the ImageBitmap, in sequence, or by specific offset(s)
console.log(new Uint8Array(buffer)); // Uint8Array(33164) [82, 73, 70, 70, 28, 0, 0, 0, 87, 69, 66, 80, 86, 80, 56, 76, 16, 0, 0, 0, ...]
})
} catch (e) {
console.error(e);
}
...
})
```
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/FileAPI/issues/150#issuecomment-609477046
Received on Sunday, 5 April 2020 20:26:25 UTC