Skip to content

Instantly share code, notes, and snippets.

@jimmywarting
Last active February 20, 2024 21:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimmywarting/65c358f878cac8e7f39cfb7d43931f62 to your computer and use it in GitHub Desktop.
Save jimmywarting/65c358f878cac8e7f39cfb7d43931f62 to your computer and use it in GitHub Desktop.
Reading a blob synchronous in javascript
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