Last active
February 20, 2024 21:13
-
-
Save jimmywarting/65c358f878cac8e7f39cfb7d43931f62 to your computer and use it in GitHub Desktop.
Reading a blob synchronous in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const blob = new Blob(['123']) | |
const xhr = new XMLHttpRequest() | |
const url = URL.createObjectURL(blob) | |
// required if you need to read binary data: | |
xhr.overrideMimeType('text/plain; charset=x-user-defined') | |
xhr.open('GET', url, false) | |
xhr.send() | |
const uint8 = Uint8Array.from(xhr.response, c => c.charCodeAt(0)) | |
const text = new TextDecoder().decode(uint8) | |
const arrayBuffer = uint8.buffer | |
const json = JSON.parse(text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment