Created
December 12, 2015 14:26
-
-
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.
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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Blob has an arrayBuffer method now
blob.arrayBuffer()
which returns an array buffer directly