0

I have the code below where I upload a file and I want to get its contents in a variable. For this I use the onload function that gives me the result, which is the contents of the file. But the variable gives undefined out of onload, how to make that result be able to be used outside of this function?

if (event.target.files.length> 0) {
  let fileContent;
  let file = event.target.files [0];
  let fileReader = new FileReader ();
  fileReader.onload = function () {
      fileContent = fileReader.result;
    console.log (fileContent); //file content result
  };

  fileReader.readAsText (file);

  console.log (fileContent); //undefined
  }
}

PS: I need to know how to do it in Angular, not jQuery.

Thanks

Sabrina B.
  • 306
  • 1
  • 8
  • 23
  • this "duplicate answer " not helped my problem, someone can help? – Sabrina B. Jan 28 '19 at 12:03
  • I answered this question here: https://pt.stackoverflow.com/questions/357849/angular-7-typescript-pegar-valor-de-dentro-de-uma-fun%C3%A7%C3%A3o-onload-upload-de-ar – Sabrina B. Feb 22 '19 at 16:39

0 Answers0