javascript 读入文本文件以显示在 HTML 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30037795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:32:15  来源:igfitidea点击:

Reading in a text file to display in HTML

javascripthtmlio

提问by user279185

I am just working on a web site for fun and I was wondering if you could use JavaScript to read in a local text file and then display it HTML. So for example if I had a text file saved with my project then could I write a function in JavaScript that reads the entire file and then just displays all that text within on the web page?

我只是为了好玩而在一个网站上工作,我想知道您是否可以使用 JavaScript 读取本地文本文件,然后将其显示为 HTML。例如,如果我有一个与我的项目一起保存的文本文件,那么我可以用 JavaScript 编写一个函数来读取整个文件,然后只在网页上显示所有文本吗?

I've looked around and tried to find something that will do this, but I don't think I fully understand. I have:

我环顾四周并试图找到可以做到这一点的东西,但我不认为我完全理解。我有:

function readFile(fileName){
   var fileRead = new ActiveXObject('Scripting.FileSystemObject');
   text = fileRead.OpenTextFile(fileName, 1, false, 0);

   while(!text.AtEndOfSteam){
       text += text.ReadLine();
    }

   text.Close();    
}

I don't know if this is will work or do what I want to do. So my question is, how do I use JavaScript to read in a text file, then display the contents of that text file on a webpage in HTML?

我不知道这是否可行或做我想做的事。所以我的问题是,如何使用 JavaScript 读取文本文件,然后以 HTML 格式在网页上显示该文本文件的内容?

回答by Bertrand KHE

You have to use the File API.

您必须使用文件 API。

var input = document.getElementById("myFile");
var output = document.getElementById("output");

input.addEventListener("change", function () {
  if (this.files && this.files[0]) {
    var myFile = this.files[0];
    var reader = new FileReader();
    
    reader.addEventListener('load', function (e) {
      output.textContent = e.target.result;
    });
    
    reader.readAsBinaryString(myFile);
  }
});
<input type="file" id="myFile">

<div id="output"></div>

EDIT

编辑

After reading your comment, i think this is what you want.

阅读您的评论后,我认为这就是您想要的。

var output = document.getElementById("output");

$("a").on("click", function (e) {
  e.preventDefault();
  $.ajax(this.href).done(function(data) {
    output.textContent = data;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"></div>

<a href="link/to/file">Link to file</a>

回答by Poornachander K

<html>
  <head>
    <title>reading file</title>
   </head>
<body>


<input type="file" id="myFile">
<hr>
<!--<div style="width: 300px;height: 0px" id="output"></div>-->
<textarea style="width:500px;height: 400px" id="output"></textarea>

<script>
var input = document.getElementById("myFile");
var output = document.getElementById("output");

input.addEventListener("change", function () {
  if (this.files && this.files[0]) {
    var myFile = this.files[0];
    var reader = new FileReader();

    reader.addEventListener('load', function (e) {
      output.textContent = e.target.result;
    });

    reader.readAsBinaryString(myFile);
  }
});
</script>
  </body>
</html>