I need some help returning the "bytes" variable from this function below to be used as input in another function.
function openfile() {
var input = document.getElementById("files").files;
var fileData = new Blob([input[0]]);
var reader = new FileReader();
reader.readAsArrayBuffer(fileData);
reader.onload = function(){
var arrayBuffer = reader.result
var bytes = new Uint8Array(arrayBuffer);
console.log(bytes);
}
I'd like to get the return of the above function and use the array of bytes as input parameter in another function.
You can use promises to wait for the file reader to finish loading your file.
Here you can find more information on promises.
Here is an example on how you could integrate a promise into your situation.
If you pass the
onload
function the event, you can make it work.This corrects it from
reader.result
toe.target.result;
.Additionally, there's a problem in using
fileData
, which is set toBlob[files[0]]
and sending that toreader.readAsArrayBuffer
. RemovefileData
and call it withreader.readAsArrayBuffer(input[0]);
, instead.