Reading a local file using JavaScript


Hello!
I immediately ask you not to throw your slippers-I honestly Googled, but I did not find an adequate answer...

I need to read the file specified by the user (from the local machine, of course) in JavaScript. The task is, not my fantasy...

It seems that this can be done by means of HTML5 (File API), and by the way, this is normally done...
But the ubiquitous IE as always spoils everything, and therefore you need the alternative is at least for him, because he File API still doesn't support it...
Of course, ideally, you need a cross-browser alternative, if there is one...

Actually, this is the whole question for you, experts... in the alternative...

Author: Дух сообщества, 2013-03-27

5 answers

A piece of code can be added "head-on" - via src. Just don't forget what you wrote there. For example, a text file may contain a variable like:

var имя="строка"; (можно без "var" и ";")
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<script src="myfile.txt" type="text/javascript"></script> 
</head>
<body>
<!-- Содержимое файла "myfile.txt": 'stroke="My text."' -->
Значение переменной "stroke": <script>document.write(stroke);</script>
</body>
</html>
 2
Author: user214777, 2016-07-09 10:53:13

For IE-ActiveXObject not ?

var inp = document.getElementById('myFile');

var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile(inp.value, 1);

while (!f.AtEndOfStream) {
    var r = f.ReadLine();
    document.write (r + "<br />");
}

f.Close();
 3
Author: walik, 2013-03-27 22:09:18

Guys! I don't know if the topic is still relevant or not, but I found a solution! On the site CodePen there was one project where an SVG image was loaded and parsed there. I stole the code from there.

function readFile(object) {
  var file = object.files[0]
  var reader = new FileReader()
  reader.onload = function() {
    document.getElementById('out').innerHTML = reader.result
  }
  reader.readAsText(file)
}
<input type="file" id="file">
<button onclick="readFile(document.getElementById('file'))">Read!</button>
<div id="out"></div>

That's something like that. You can look at CodePen version

  • reads ALL files
 2
Author: Alexey Pinaew, 2017-03-31 17:32:06

JS does not support this.
HTML5 really supports this and soon even IE will learn it-wait a year or two...
If you need it earlier, then ActiveX, and for older versions of the browser, only Flash.

As they say, more is not given. The browser is initially limited in this regard.
For specific solutions, Google will help you, since you did not say what tools you use - pure JS, or not...

 0
Author: t1nk, 2014-05-31 15:22:54

Why HTML5 ?

$.ajax({
      url: 'filename.dat',
      success: function(data) {
         console.log(data); // Выведет содержимое файла в консоль
      }
});

If you just need to get the info from the file-theoretically it should work, after all .json files are parsed without problems.

 -1
Author: andrewshka, 2013-03-27 22:21:19