somedude
somedude

Reputation: 119

fileReader.readAsText() throwing 'FileReader': parameter 1 is not of type 'Blob'

I'm trying to read a CSV or Excel file in order to convert it to table. For now I just created a class in order to read the input file:

The input is:

<input type="file" class="form-control-file" id="file_upload">

For now I just created a class in order to read the input file:

 class InputReader_tiqet{

    constructor(input_id) {
        this.inputId=input_id;
        this.filetoLoad=document.getElementById("file_upload").files[0];
    }   

    readFileData(){
        this.filetoLoad = document.getElementById(this.inputId).files[0];
        var fileReader = new FileReader();
        fileReader.onload = function(fileLoadedEvent){
            this.fileData = fileLoadedEvent.target.result;

        };

        fileReader.readAsText(this.fileToLoad, "UTF-8");
    }

    getData(){
        return this.fileData;
    }


}

I run it as it follows:

inputReader_object= new InputReader_tiqet("file_upload");

 $("#file_upload").on('change', function(){
            inputReader_object.readFileData();
            //clean input field
            this.val("");
});

When the input file changes I get the following error:

Uncaught TypeError: Failed to execute 'readAsText' on 'FileReader': parameter 1 is not of type 'Blob'.

Upvotes: 0

Views: 1036

Answers (1)

somedude
somedude

Reputation: 119

problem solved: this.filetoLoad instead of this.fileToLoad.

Upvotes: 1

Related Questions