Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created the below 2 things and try to read file content but not able to get content and getting errors like files

<input type='file' accept='text/xml' (change)='openFile(fileupload)'">

openFile(fileupload) {
    let input = fileupload.target;
    for (var index = 0; index < input.files.length; index++) {
        let reader = new FileReader();
        reader.onload = () => {
            // this 'text' is the content of the file
            var text = reader.result;
        }
        reader.readAsText(input.files[index]);
    };


What I have tried:

I have tried with these

<input type='file' accept='text/xml' (change)='openFile(fileupload)'">

openFile(fileupload) {
    let input = fileupload.target;
    for (var index = 0; index < input.files.length; index++) {
        let reader = new FileReader();
        reader.onload = () => {
            // this 'text' is the content of the file
            var text = reader.result;
        }
        reader.readAsText(input.files[index]);
    };


errors:
Cannot read property 'files' of undefined


Could you please help on this that how to get the content of any file which has uploaded from file upload control.
Posted
Updated 2-Feb-17 3:57am

1 solution

Change your input to:
<input type='file' accept='text/xml' onchange='openFile(this)'">

Apart from the syntax error in your attribute, you're trying to pass a global variable called fileupload to your function. But no such variable exists.

You'll also need to change your function, since the input is passed directly to the function:
openFile(fileupload) {
    let input = fileupload; // Remove: .target;
    ...
};
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900