Adrià Vilanova MartÃnez | 7f1e8ea | 2022-10-14 15:50:11 +0200 | [diff] [blame] | 1 | // Source: https://stackoverflow.com/a/66046176 |
| 2 | export const arrayBufferToBase64 = async (data) => { |
| 3 | // Use a FileReader to generate a base64 data URI |
| 4 | const base64url = await new Promise((r) => { |
| 5 | const reader = new FileReader(); |
| 6 | reader.onload = () => r(reader.result); |
| 7 | reader.readAsDataURL(new Blob([data])); |
| 8 | }); |
| 9 | |
| 10 | /* |
| 11 | The result looks like |
| 12 | "data:application/octet-stream;base64,<your base64 data>", |
| 13 | so we split off the beginning: |
| 14 | */ |
| 15 | return base64url.split(',', 2)[1] |
| 16 | } |