Skip to content

Instantly share code, notes, and snippets.

@davemackintosh
Created December 12, 2015 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davemackintosh/fc73a105c6cbf790cbe6 to your computer and use it in GitHub Desktop.
Save davemackintosh/fc73a105c6cbf790cbe6 to your computer and use it in GitHub Desktop.
Electron blob to buffer, I made this to use with the nativeImage class.
function blob_to_buffer(blob, callback) {
// Create a file reader instance.
const file_reader = new FileReader()
// Listen to when reading is finished and
// run the callback with a buffer.
file_reader.addEventListener("loadend", event => {
if (event.error) {
callback(event.error)
}
else {
callback(null, new Buffer(file_reader.result))
}
// Remove the listener.
file_reader.removeEventListener("loadend", blob_to_buffer, false)
}, false)
// Read the blob as a typed array.
file_reader.readAsArrayBuffer(blob)
return file_reader
}
@wizebin
Copy link

wizebin commented Dec 19, 2020

Blob has an arrayBuffer method now

blob.arrayBuffer()

which returns an array buffer directly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment