I am trying to extract the byteArray for the file which is being selected from my Angular app. I am using "readAsArrayBuffer" property of FileReader Class to get the file as an Array Buffer, the output for which is below:
Below is the Code Sample for the above output:
onFileSelected(event)
{
if (event.target.files && event.target.files.length > 0)
{
let reader = new FileReader();
if(event.target.files && event.target.files.length > 0) {
let file = event.target.files[0];
reader.readAsArrayBuffer(file);
reader.onload = () => {
this.form.get('file').setValue({
filename: file.name,
filetype: file.type,
value:reader.result
})
};
}
}
else
{
console.log("Error");
}
}
I am trying to get the Uint8Array from the below statement :
value:String.fromCharCode.apply(null, new Uint8Array(reader.result))
Below is the code for the same. Its the same as above with a minor change in the "value" attribute.
onFileSelected(event)
{
if (event.target.files && event.target.files.length > 0)
{
let reader = new FileReader();
if(event.target.files && event.target.files.length > 0) {
let file = event.target.files[0];
console.log(file);
reader.readAsArrayBuffer(file);
reader.onload = () => {
this.form.get('file').setValue({
filename: file.name,
filetype: file.type,
value:String.fromCharCode.apply(null, new Uint8Array(reader.result))
})
};
}
}
else
{
console.log("Error");
}
}
The output for which is below. Its an Error:
i tried the implementing the resolution given at the below thread but was unsuccessful in doing so:
Maximum call stack size exceeded error
I need my file as a byte array so that i can pass it to a REST API which only accepts the file as a byteArray. Thanks !