eclipsesource / tabris-js

Create native mobile apps in JavaScript or TypeScript.
https://tabrisjs.com
BSD 3-Clause "New" or "Revised" License
1.4k stars 170 forks source link

Support the FileReader web API #2226

Open cpetrov opened 2 years ago

cpetrov commented 2 years ago

Feature description

The FileReader API enables conversion of Blobs into ArrayBuffer, binary and text strings and data URLs. It is particularly useful when serializing data for exchange with external services.

Proposed solution

Implement the FileReader API.

cpetrov commented 2 years ago

Most of this is already possible with Tabris. Constructing the data URL string requires a btoa polyfill like abab and knowledge of the media type though.

const arrayBuffer = await blob.arrayBuffer();
const binaryString = await blobToBinaryString(blob);
const textString = await blob.text();
const dataUrlString = 'data:<my-media-type>;base64,' + btoa(await blobToBinaryString(blob));

async function blobToBinaryString(blob) {
    const ab = await blob.arrayBuffer();
    return new Uint8Array(ab).reduce((data, byte) => data + String.fromCharCode(byte), '')
}