reactJS and reading a text file

javascript read text file into string
read text file and display in html using javascript
javascript read text file line by line
javascript read text file into array
read text file javascript
javascript read file contents into variable
reactjs write to text file
html read text file

I have a reactJS application where I need to read in a large text file and use the data from the file to populate some arrays. I came across some code examples and I am trying to implement this code:

        readTextFile = file => {
        var rawFile = new XMLHttpRequest();
        rawFile.open("GET", file, false);
        rawFile.onreadystatechange = () => {
            if (rawFile.readyState === 4) {
                if (rawFile.status === 200 || rawFile.status == 0) {
                    var allText = rawFile.responseText;
                    console.log("allText: ", allText);
                    this.setState({
                        fundData: allText
                    });
                }
            }
        };
        rawFile.send(null);
    };

The code is called by executing this line of code:

this.readTextFile("../text/fund_list.txt");

When I execute the application, I get the following error message:

GET http://localhost:8080/text/fund_list.txt 404 (Not Found)

This is what my application structure looks like:

The readTextFile function is in the account_balanc.js code and I am trying to read in the text file /text/fund_list.txt. The file does exist so obviously I am not referencing the file correctly but I don't know why.

I have tried this.readTextFile("../text/fund_list.txt") and this.readTextFile("./text/fund_list.txt"); but neither worked. I even tried moving fund_list.txt into the /screens folder and changing the function call to this.readTextFile("fund_list.txt"); but I still get the 404 error message.

Any ideas why?

Thank you.


I moved the fund_list.txt file into the public/text folder (after I created it) and changed the calling of the function to this.readTextFile("./text/fund_list.txt");

I saw this as a posted solution but then the post was removed (I don't know why) so I can't thank the user who posted it. But this solved my problem.

A React component to read text file. · GitHub, import React from "react";. /*. Read a text file and out put the content. Example Usage: var myTxt = require("./myTxt.txt"); <TextFileReader. txt={myTxt}. />. */. In node.js these are one library fs (File-System) which is used to manage all read and write operations. By using the fs module we can read and write files in both synchronous and asynchronous way. There are many ways in which we can read and write data to file. Lets have a look on each of them one by one. JavaScript Read and Write to Text File


You can use webpack raw loader and directly import the .txt file into the component.

First, install raw-loader:

npm i -D raw-loader

Second, add the loader to your webpack config:

// webpack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /\.txt$/,
        use: 'raw-loader'
      }
    ]
  }
}

Then, directly import .txt to your component:

import txt from './file.txt';

react-read-text-file, Starter project for React apps that exports to the create-react-app CLI. 4.2k. 18. Files. Hello.js. Rename. Delete. index.html. Rename. Delete. index.js. Rename. If you look at the example closely (this is just a snippit of the entire thing) you'll notice that the file is coming from this.state.files <= this is an array where this.state.files is the accepted files in the onDrop that you got there


I don't have a lot of experience with pure javascript applications, I usually use a rails backend, but I would recommend focusing on just being able to hit http://localhost:8080/text/fund_list.txt in a browser and get a response. Once you solve that it looks like the javascript will do it's thing.

Front-end Shorts: How to Read Content From The File Input in React, result is not null, the constant content contains the contents of the file as a text string. Any load event of fileReader object sets the result value  Given a text file, write a JavaScript program to extract the contents of that file. There is a built-in Module or in-built library in NodeJs which handles all the reading operations called fs (File-System). It is basically a JavaScript program (fs.js) where function for reading operations is written. Import fs-module in the program and use


Try...

const file = require("../text/fund_list.txt");

Then

this.readTextFile(file);

Basic File reader with React JS - Habib Mahbub, Hello, this is second of my stories and i will show you how to create “Basic file reader with react js” for previewing a file before it uploaded. 1.Create a new file  The file contents are stored in the FileReader's result string property. Being a string, it allows you to manipulate it just as you would any string. For instance, you can see it in the code above where I displayed the first line. Reading Multiple Files and Properties Using a Closure


I'm assuming you are using Webpack as the bundler.

The reason you were getting a 404 Not Found is because you need to configure Webpack to bundle the file into the build folder the website is hosting.

Moving it to the /public folder worked because Webpack was configured to bundle all the files in the /public folder as it is usually the entry point of the project (since this is probably create-react-app).

Also the folks at create-react-app are smart and saw that if it was a .txt extension file, they would bundle them into a /text folder in the build file, just as any .jpg or .png files will bundle into a /images folder.

Note that a lot of files need a special webpack loader if you import them in project, but you can also use a webpack plugin to copy the file into your bundle and then read that programmatically as you are doing.

Therefore once you moved it to that folder your application could actually find it in the build folder -hosted in localhost:8080 !

Javascript, How to remove all Non-ASCII characters from the string using JavaScript ? Most visited in Web Technologies. ReactJS  Then the readAsText method is used to read the contents of the specified File or Blob. When the read operation is complete, the state is changed to done; the onloadend is triggered, and, if fileReader.result is not null, the constant content contains the contents of the file as a text string.


react-file-reader, A flexible ReactJS component for handling styled HTML file inputs. Install. npm install react-file-reader --save. ChangeLog. 1.1  In order to read a local file text through JavaScript using chrome, the chrome browser should run with the argument --allow-file-access-from-files to allow JavaScript to access local file, then you can read it using XmlHttpRequest like the following:


Read Local Text File using JavaScript FileReader API, Learn to read a text file from user's local device JavaScript File API. I love to create modern web and mobile apps using React JS, Angular 2+,  In HTML, an <input type="file"> lets the user choose one or more files from their device storage to be uploaded to a server or manipulated by JavaScript via the File API. < input type = " file " /> Because its value is read-only, it is an uncontrolled component in React.


Read specific .txt file and store its value in a string. : reactjs, Hello new to reactJs so any input would be helpful. I have a text file it has just one line of text. (Whenever the front-end starts -npm start. The txt file is updated  A React component to read text file. GitHub Gist: instantly share code, notes, and snippets.