简体   繁体   English

使用 javascript 读取 .txt 文件

[英]Read .txt file with javascript

Can you read a txt file in javascript with the file path in the code?你能用代码中的文件路径读取javascript中的txt文件吗? Not by selecting the file from the open file window.不是通过从打开的文件窗口中选择文件。

If the textfile is on a local or private PC如果文本文件在本地或私人 PC 上

As you may understand if you could read a textfile that is stored on a user's filesystem everyone would be able to steal private data, so in short NO you can't .正如您可能理解的那样,如果您可以读取存储在用户文件系统上的文本文件,那么每个人都可以窃取私人数据,因此简而言之,您不能

If your textfile is on your server如果您的文本文件在您的服务器上

ajax阿贾克斯

function ajax(a,b,c){ // Url, Callback, just a placeholder
 c=new XMLHttpRequest;
 c.open('GET',a);
 c.onload=b;
 c.send()
}

how to use:如何使用:

ajax('http://YOURSERVER/yourtextfile.txt',function(){
 alert(this.response);
});

more about the above function有关上述功能的更多信息

If your textfile is on a different server如果您的文本文件在不同的服务器上

When you tray to access other servers with ajaxyou need to be allowed to access that file.当您使用 ajax 访问其他服务器时,您需要被允许访问该文件。 some sites allow it by returning一些网站通过返回来允许它

Access-Control-Allow-Origin: *

in the response heaeders.so the above ajax function would work properly.在响应 headers.so 上面的 ajax 函数将正常工作。


Then there are other ways to get data from your or other servers that i prefer over ajax:然后还有其他方法可以从您或其他服务器获取数据,我更喜欢 ajax:

websockets & SSE .. but those need a specific interface like php or nodejs. websockets & SSE .. 但那些需要一个特定的接口,如 php 或 nodejs。

Another option is if the file is on your private pc and you just want to send some data that is stored in a specific textfile everytime you update:另一种选择是,如果文件在您的私人电脑上,而您只想在每次更新时发送一些存储在特定文本文件中的数据:

Just install nodejs or a free PHP server and create some sort of cron job to check a specific folder everyonce a while.只需安装nodejs或免费的 PHP 服务器并创建某种 cron 作业来每隔一段时间检查特定文件夹。 When the file is updated just send it to your online server.文件更新后,只需将其发送到您的在线服务器即可。 Again the online host needs at least PHP ASP or some sort of serverside scripting.同样,在线主机至少需要 PHP ASP 或某种服务器端脚本。

you can use XHR (XML Http Request).您可以使用 XHR(XML Http 请求)。 Below is an example for reading a '.txt' file.下面是读取“.txt”文件的示例。 You can have this in an html file and will need to run a simple server using NodeJS , Python Simple Server or any other server that you are comfortable with.您可以将其保存在html文件中,并且需要使用NodeJSPython Simple Server或您熟悉的任何其他服务器运行一个简单的服务器。

1 If you have python: 1如果你有蟒蛇:

python -m SimpleHTTPServer 8000

2 Example (readtxt.html): 2示例(readtxt.html):

<script type="text/javascript">
var request = new XMLHttpRequest();
request.open('GET', 'filename.txt', false);  // `false` => synchronous request
request.send(null);

if (request.status === 200) {
  console.log(request.responseText);
}
</script>

3 Open: http://localhost:8000/readtxt.html 3打开: http://localhost:8000/readtxt.html

4 Check the browser console for content read from the txt file. 4检查浏览器控制台是否有从 txt 文件中读取的内容。

You can read this for more detailed information,您可以阅读本文以获取更多详细信息,

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests#Asynchronous_request https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests#Asynchronous_request

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM