I am trying to convert an uploaded file into a string using DOMParser. To do this, i am using two methods as follows:
method to upload a file
handleFileInput(event){
setTimeout(async ()=>{
if(event.target.files.item(0)!=null){
this.uploadedFile=event.target.files.item(0);
this.XMLAsString= await this.convertFileToString(this.uploadedFile);
console.log("XML as String-> ",this.XMLAsString);
}
}, 3500);
}
method to convert a file to string
convertFileToString(file):String{
let fileInString:String=null;
let fileReader = new FileReader();
fileReader.onload = (event) =>{
fileInString=fileReader.result as String
}
fileReader.readAsText(file);
//console.log("XML as String inside convert method-> ",this.XMLAsString);
if(fileInString!=null){
return fileInString
}
}
But when i try print the value of XMLAsString
in handleFileInput(event)
(in line 6) function, it returns undefined. I understand this is happening due to the fileReader.onload
but now, how do i fetch the value of fileInString
in XMLAsString
?
NOTE: I need to keep the setTimeOut
as it is, as i need it to do some other tasks.