Skip to content

Instantly share code, notes, and snippets.

@1j01
Forked from davemackintosh/blob_to_buffer.js
Last active August 24, 2018 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1j01/2ee0cb5b44e770abcdb8fda92db64783 to your computer and use it in GitHub Desktop.
Save 1j01/2ee0cb5b44e770abcdb8fda92db64783 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) {
const file_reader = new FileReader()
file_reader.addEventListener("loadend", event => {
if (file_reader.error) {
callback(file_reader.error)
} else {
callback(null, new Buffer(file_reader.result))
}
}, false)
// Read the blob as a typed array.
file_reader.readAsArrayBuffer(blob)
// ⚠ Always test your error handling!
// You can use this to do so!
// file_reader.abort()
// (When there's not an easy way to cause something to actually fail,
// you can at least test sending an error callback, btw)
return file_reader
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment