Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8933911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:39:33+00:00 2026-06-15T09:39:33+00:00

I am trying to load a text file into my JavaScript file and then

  • 0

I am trying to load a text file into my JavaScript file and then read the lines from that file in order to get information, and I tried the FileReader but it does not seem to be working. Can anyone help?

function analyze(){
   var f = new FileReader();

   f.onloadend = function(){
       console.log("success");
   }
   f.readAsText("cities.txt");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:39:35+00:00Added an answer on June 15, 2026 at 9:39 am

    Yeah it is possible with FileReader, I have already done an example of this, here’s the code:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Read File (via User Input selection)</title>
        <script type="text/javascript">
        var reader; //GLOBAL File Reader object for demo purpose only
    
        /**
         * Check for the various File API support.
         */
        function checkFileAPI() {
            if (window.File && window.FileReader && window.FileList && window.Blob) {
                reader = new FileReader();
                return true; 
            } else {
                alert('The File APIs are not fully supported by your browser. Fallback required.');
                return false;
            }
        }
    
        /**
         * read text input
         */
        function readText(filePath) {
            var output = ""; //placeholder for text output
            if(filePath.files && filePath.files[0]) {           
                reader.onload = function (e) {
                    output = e.target.result;
                    displayContents(output);
                };//end onload()
                reader.readAsText(filePath.files[0]);
            }//end if html5 filelist support
            else if(ActiveXObject && filePath) { //fallback to IE 6-8 support via ActiveX
                try {
                    reader = new ActiveXObject("Scripting.FileSystemObject");
                    var file = reader.OpenTextFile(filePath, 1); //ActiveX File Object
                    output = file.ReadAll(); //text contents of file
                    file.Close(); //close file "input stream"
                    displayContents(output);
                } catch (e) {
                    if (e.number == -2146827859) {
                        alert('Unable to access local files due to browser security settings. ' + 
                         'To overcome this, go to Tools->Internet Options->Security->Custom Level. ' + 
                         'Find the setting for "Initialize and script ActiveX controls not marked as safe" and change it to "Enable" or "Prompt"'); 
                    }
                }       
            }
            else { //this is where you could fallback to Java Applet, Flash or similar
                return false;
            }       
            return true;
        }   
    
        /**
         * display content using a basic HTML replacement
         */
        function displayContents(txt) {
            var el = document.getElementById('main'); 
            el.innerHTML = txt; //display output in DOM
        }   
    </script>
    </head>
    <body onload="checkFileAPI();">
        <div id="container">    
            <input type="file" onchange='readText(this)' />
            <br/>
            <hr/>   
            <h3>Contents of the Text file:</h3>
            <div id="main">
                ...
            </div>
        </div>
    </body>
    </html>
    

    It’s also possible to do the same thing to support some older versions of IE (I think 6-8) using the ActiveX Object, I had some old code which does that too but its been a while so I’ll have to dig it up I’ve found a solution similar to the one I used courtesy of Jacky Cui’s blog and edited this answer (also cleaned up code a bit). Hope it helps.

    Lastly, I just read some other answers that beat me to the draw, but as they suggest, you might be looking for code that lets you load a text file from the server (or device) where the JavaScript file is sitting. If that’s the case then you want AJAX code to load the document dynamically which would be something as follows:

    <!DOCTYPE html>
    <html>
    <head><meta charset="utf-8" />
    <title>Read File (via AJAX)</title>
    <script type="text/javascript">
    var reader = new XMLHttpRequest() || new ActiveXObject('MSXML2.XMLHTTP');
    
    function loadFile() {
        reader.open('get', 'test.txt', true); 
        reader.onreadystatechange = displayContents;
        reader.send(null);
    }
    
    function displayContents() {
        if(reader.readyState==4) {
            var el = document.getElementById('main');
            el.innerHTML = reader.responseText;
        }
    }
    
    </script>
    </head>
    <body>
    <div id="container">
        <input type="button" value="test.txt"  onclick="loadFile()" />
        <div id="main">
        </div>
    </div>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to load a list of filenames from a text file and then
I have been trying to load a text file into a combobox, and then
Hei guys, I'm trying to load a long text from a .rtf file and
I'm trying to load text of all divs that have a particular class into
Trying to load a small .txt file into mysql but get all my data
I'm trying to load a large text file into MATLAB. The file has the
I am trying to load javascript files into webview. Some of that files are
I'm trying to load a url of an xml file into my page then
I'm trying to load data from a text file on my server using the
I'm trying only to load a part of a text file (*.txt) into the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.