The FileReader's onload event handler does not seem to be triggering as expected

In short, my issue revolves around reading a csv file from an android device using JavaScript's FileReader. Although my code was functioning properly a month ago, upon revisiting it recently I discovered that the onload function no longer seems to be triggering.

When checking the console log, I observed that the object does indeed have the onload function loaded, and both the file itself as well as its content are being read. Below, I will provide snippets of the relevant code:

readCSVFile(input: HTMLInputElement) {

    var content = this.csvContent;
    const files = input.files;
    console.log("files: ", files);

    if (files && files.length) {

      const fileToRead = files[0];
      console.log("step1");
      var fileReader = new FileReader();

      console.log("state 1: ", fileReader.readyState); //prints undefined, but should have been "0"

      fileReader.onload = this.onFileLoad.bind(this);
      console.log("fileReader: ", fileReader);
      fileReader.readAsText(fileToRead, "UTF-8");
      console.log("fileReader 2: ", fileReader.onload);
      console.log("state 3: ", fileReader.readyState);
   }
}

I'm inclined to believe that the 'onFileLoad' function is not the culprit, even though its first line contains a console.log("something") statement which isn't appearing in the console output. Here are some noteworthy excerpts from the console log:

fileReader 2:  Æ’ (fileLoadedEvent) {
        console.log("step2");
        var textFromFileLoaded = fileLoadedEvent.target.result;
        this.csvContent = textFromFileLoaded;
        console.log("Continut: ", t…

state 1:  undefined

Answer â„–1

After reviewing the output of FileReader.toString() described in your comment

"function () {
  this._readyState = 0;
  this._error = null;
  this._result = null;
  this._progress = null;
  this._localURL = '';
  this._realReader = origFileReader ? new origFileReader() : {}; // eslint-disable-line new-cap
}"

This indicates that you are working with a wrapper object instead of the standard web-API's FileReader.

If this wrapper is causing issues by overriding the original behavior, it may be best to remove it entirely. However, if you wish to retain it, you can still interact with its ._realReader property as it appears to function like a typical FileReader.

Answer â„–2

One way to approach this is by not using closure and instead doing something like the following: fileReader.onload = function(ev){ //insert your code here }.bind(this);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Utilizing ng class with an array of objects: A step-by-step guide

I am facing a scenario in which my response appears as follows: "currency" : [ { "_id" : ObjectId("584aad5d3e2537613e5f4c39"), "name" : "USD" } ], I need to enable my checkbox based on the currency name. I attempted the followi ...

Is there a way to bring together scrolling effects and mouse movement for a dynamic user

If you want to see a scrolling effect, check out this link here. For another animation on mouse move, click on this link(here). Combining both the scrolling effect and the image movement might seem challenging due to different styles used in each templat ...

React is unable to locate an import statement for Material UI components

I am facing an issue while incorporating material UI components into my React project. The error message I receive is related to an invalid import. Snippet from My Component File import React from 'react' import './middle.css' import Mi ...

insert a new user into a MongoDB collection

I am facing an issue with adding a user to my collection called users. The code I'm currently using is: var user1 = { Username: "joonyoftv", Password: "joon123", Email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cf ...

How to break out of an endless loop in Node.js and Express.Js

I have developed an Express app that is designed to paginate through an external API call. I have thoroughly examined the code but for some reason, the loop condition to break the function isn't being triggered. Any assistance on this matter would be ...

Understanding the Document.ready function?

Recently, I came across some websites that follow this specific pattern: <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script> $(function (){...do some stuff with p ...

Pressing the ENTER key does not submit the text box data in Rails/Bootstrap, but clicking the SUBMIT button does

I need assistance with my Rails 4 and Bootstrap 3 project. I have implemented an email address field on my page (localhost:3000) where users can enter their email and click the SUBMIT button to send it to the MongoDB database. However, pressing the ENTER k ...

What is the best method for obtaining accurate normal values in three.js?

I'm having trouble understanding how normals are computed in Three.js. Here is the issue I am facing: I have created a simple plane using the following code: var plane = new THREE.PlaneGeometry(10, 100, 10, 10); var material = new THREE.MeshBasicMa ...

Rearrange the layout by dragging and dropping images to switch their places

I've been working on implementing a photo uploader that requires the order of photos to be maintained. In order to achieve this, I have attempted to incorporate a drag and drop feature to swap their positions. However, I am encountering an issue where ...

Making a Zoom effect using p5.js

I have searched for a solution to this question multiple times, but none of the answers I came across seem to work for me. Currently, I am able to allow the user to scale an image with a simple scale(factor) call. However, now I am attempting to implement ...

Is it possible for me to display dynamic content and utilize a template engine like (ejs)?

Currently, I am in the process of developing a weather application to enhance my skills with express and ejs. At the moment, I'm utilizing app.get to fetch data from darkSky's API successfully, and I can display it on my index.ejs page. My next ...

JavaScript Array Problem

Could you please review the code below and help me understand why I am encountering issues when trying to run the program? $(document).ready(function() { var comp = new Array("AAPL", "MSFT", "XRTX&"); var t = setInterval(function(){ ...

Refresh React Components on the Fly (Solr)

I am relatively new to ReactJS In my React class, I have a function that is rendering multiple items: (Sample) var app = app || {}; app.Results = React.createClass({ componentDidMount: function () { }, handleUpdateEvent: function(id) ...

Relay information between requests using a RESTful API and various data formats, such as JSON, XML, HTML

In a scenario with a REST API that can deliver JSON, XML, HTML, and other formats, the default response for browsers without JavaScript enabled is HTML. This API utilizes tokens for authentication and authorization. Within a traditional website project, t ...

How can I retrieve the row index in an Angular Mat-Table that has expandable content?

Having a mat-table with expandable content on a page, I am seeking a solution to record the row number of the table when clicked. While I successfully achieved this with a regular table in the HTML file using: <tr mat-row *matRowDef="let row; columns: ...

Steps for integrating Stanford's NLP into a web application

I have successfully developed a project utilizing Stanford's NLP API and models. Now, I am looking to integrate this Java-based project onto the web. After seeing the demo provided by Stanford-NLP, I am curious about the process they use to achieve th ...

Automatically scrolling through a nested list in ReactJs

I have a chat list integrated into my web application. The entire webpage is scrollable, as shown in the image at the bottom. I would like the messages list to automatically scroll to the bottom whenever a new message arrives. To achieve this, I created a ...

Searching for true values in MongoDB using the query syntax can be challenging

I have a question that might be a bit embarrassing, but I need help with rendering a user search based on both location and date. Our profile object is structured like this: availability: { monday: { type: Boolean, default: false }, tuesday: { type ...

The default value for an input of type date should be set to the current date

I am working on a project that involves an input field with the type of "date". I have implemented Materialize to provide a user-friendly date picker. My goal is to set the default value of this input field to the current date when it is initialized. Here ...

What is the process for adjusting the expiration time of a GitLab accessToken?

I am currently using NextAuth for logging in with GitLab, but I am encountering an issue where my accessToken changes every 2 hours. How can I ensure that it remains valid for a longer period so that I can successfully store it in my database? It's wo ...