I have the following Typescript function that when called opens a window to request a file, and after the user makes a selection it prints in the console the file contents.
The first time it works fine, but the problem is that if I call it twice, it doesn't work. The second time it opens the window, but when the file is selected it does nothing. What is wrong with this function?
<input id="file-input" type="file" name="name" style="display: none;" />
printFile(){
var input = document.getElementById('file-input');
input.addEventListener('input', () => {
var uploadedFile = (input as HTMLInputElement).files[0];
var reader = new FileReader();
reader.addEventListener('load', () => {
console.log(reader.result);
});
reader.readAsText(uploadedFile);
});
input.click();
}