最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

html - How to read a text file saved on my computer using javascript - Stack Overflow

matteradmin4PV0评论

I have a text file that is being updated regularly and I want to know if it is possible to read the file, line by line, with javascript and save the values in variables and then update the html page with the new values every time the page is loaded. The html page is a simple page to display information at work, not a live web page, and does not require any user input other than just navigating between the two pages. The text file is on a network drive that everyone has access to. Here is an example of what I'm trying to acplish:

var value1;
var value2;

Read the file with javascript if possible and extract data and assign to value1 and value2.

document.getElementsByClassName("class for <p>")[0].innerHTML = value;
document.getElementsByClassName("class for another <p>")[0].innerHTML = value;        

I have googled this but was not able to find anything that worked. If this is not possible with JS, any suggestions on how this can be done differently. Thanks in advance.

I have a text file that is being updated regularly and I want to know if it is possible to read the file, line by line, with javascript and save the values in variables and then update the html page with the new values every time the page is loaded. The html page is a simple page to display information at work, not a live web page, and does not require any user input other than just navigating between the two pages. The text file is on a network drive that everyone has access to. Here is an example of what I'm trying to acplish:

var value1;
var value2;

Read the file with javascript if possible and extract data and assign to value1 and value2.

document.getElementsByClassName("class for <p>")[0].innerHTML = value;
document.getElementsByClassName("class for another <p>")[0].innerHTML = value;        

I have googled this but was not able to find anything that worked. If this is not possible with JS, any suggestions on how this can be done differently. Thanks in advance.

Share Improve this question edited Dec 15, 2016 at 3:15 deChristo 1,8782 gold badges20 silver badges30 bronze badges asked Dec 15, 2016 at 2:14 fGkfGk 511 gold badge1 silver badge4 bronze badges 1
  • 1 It is not possible to do it without a web server (apache, nginx, Node.js express...): browser JS does not have access to files on your hard drive as a security measure. – Amadan Commented Dec 15, 2016 at 2:18
Add a ment  | 

2 Answers 2

Reset to default 6

At first, you need to use a input[type=file] to get a File.

<input type=file id=file/>
<div id=result></div>

And then use FileReader to read file to target format, just like base64, text, buffer.

const file = document.getElementById('file').files[0]
const result = document.getElementById('result')
const reader = new FileReader
reader.addEventListener('load', () => {
    result.innerHTML = reader.result
})
reader.readAsText(file, 'UTF-8')

See: https://developer.mozilla/en-US/docs/Web/API/FileReader

You could use the javascript FileReader and input type="file" to read the local files in your machine. Please see the below attached code for example.

<!DOCTYPE html>
<html>

<head>
    <script>
        function OnFileLoad() {
            var file = document.getElementById("FileReader").files[0];
            var textType = /text.*/;
            var fileDisplayArea = document.getElementById("FileContent");
            if (file.type.match(textType)) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    fileDisplayArea.innerText = reader.result;
                }

                reader.readAsText(file);
            } else {
                fileDisplayArea.innerText = "File not supported!"
            }
        }
    </script>
</head>

<body>
    <input type="file" id="FileReader" onchange="OnFileLoad()" />
    <div id="FileContent">Your content will appear here</div>
</body>

</html>

In order to specify the file path you might need to have a server as well. I have attached a sample code here with. You can find the details regarding Specifying the file path here and issues that will happen to read file without a server here

<!DOCTYPE html>
<html>

<head>
    <script>
        function readTextFile(file) {
            var rawFile = new XMLHttpRequest();
            rawFile.open("GET", file, false);
            var fileDisplayArea = document.getElementById("FileContent");
            rawFile.onreadystatechange = function () {
                if (rawFile.readyState === 4) {
                    if (rawFile.status === 200 || rawFile.status == 0) {
                        var allText = rawFile.responseText;
                        fileDisplayArea.innerText = allText;
                    }
                } else {
                    fileDisplayArea.innerText = "File not supported!"
                }
            }
            rawFile.send(null);
        }
        readTextFile("file:///C:/Users/t0423/Desktop/test.js")
    </script>
</head>

<body>
    <div id="FileContent">Your content will appear here</div>
</body>

</html>

Post a comment

comment list (0)

  1. No comments so far