Javascript 使用javascript读取本地文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42088285/
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
Read local text file with javascript
提问by Markus
I want to read a local text file from my local html file, so I tried to follow the solution in this thread Javascript - read local text filebut the suggested solution does not work for me either:
我想从我的本地 html 文件中读取本地文本文件,所以我尝试按照此线程Javascript 中的解决方案进行操作- 读取本地文本文件,但建议的解决方案对我也不起作用:
function readTextFile(file)
{
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var allText = rawFile.responseText;
alert(allText);
}
}
}
rawFile.send(null);
}
When I call the function readTextFile("file:///D:/test/text.txt");no error does show up in firebug but no alert is shown neither. I use Windows und Firefox 51.0.1 (64-Bit).
I don't want to use the function FileReader()in combination with a button <input type='file' onchange='openFile(event)' ...since the text file needs to be read automatically when loading the page. So how can I make work the solution above?
当我调用该函数readTextFile("file:///D:/test/text.txt");时,萤火虫中没有显示任何错误,但也没有显示任何警报。我使用 Windows 和 Firefox 51.0.1(64 位)。我不想将该功能FileReader()与按钮结合使用,<input type='file' onchange='openFile(event)' ...因为在加载页面时需要自动读取文本文件。那么我怎样才能使上述解决方案起作用呢?
Reading the thread linked it looks like others also have problems with that solution although the thread is marked as solved.
阅读链接的线程看起来其他人也有该解决方案的问题,尽管该线程被标记为已解决。
回答by Balazs Vago
Complete HTML and JavaScript file as an example for reading client side data files. Client side files can only be accessed by the FileReader, specifying a user selected file.
完整的 HTML 和 JavaScript 文件作为读取客户端数据文件的示例。客户端文件只能由 FileReader 访问,指定用户选择的文件。
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function loadFile(o)
{
var fr = new FileReader();
fr.onload = function(e)
{
showDataFile(e, o);
};
fr.readAsText(o.files[0]);
}
function showDataFile(e, o)
{
document.getElementById("data").innerText = e.target.result;
}
</script>
</script>
</head>
<body>
Select file to read <input type="file" onchange="loadFile(this)">
<pre id="data"></pre>
</body>
</html>
回答by edcs
As a general rule, it's not possible to access the local file system from JavaScript, so your code example shouldn't and couldn't work because of browser security.
作为一般规则,无法从 JavaScript 访问本地文件系统,因此您的代码示例不应该也不可能因为浏览器安全性而工作。
However, there's the Fileand FileReaderAPI which would allow you to read the contents of a file from an <input type="file" />and is, in reality, your only option for this sort of thing - You could use FileReader.readAsText()to access the files contents. This is a good resource for further information:
但是,有一个File和FileReaderAPI 可以让您从一个文件中读取文件的内容<input type="file" />,实际上,这是您处理此类事情的唯一选择 - 您可以使用它FileReader.readAsText()来访问文件内容。这是获取更多信息的好资源:
回答by Markus
The easiest way to solve my problem is to change the text file to a .jsfile, save it in the same folder and include it in the html file by <script src="test.js"></script>
解决我的问题最简单的方法是将文本文件更改为.js文件,将其保存在同一文件夹中并通过以下方式将其包含在html文件中<script src="test.js"></script>

