简体   繁体   English

如何在 JavaScript 中从文件系统读取文本文件

[英]How to read text file from filesystem in JavaScript

I have tried different approaches to read a text file from the local file system in JavaScript and display the content of the file in alert() but all to no avail.我尝试了不同的方法从 JavaScript 中的本地文件系统读取文本文件,并在alert()显示文件的内容,但都无济于事。

Approach 1方法一

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.response;
                     document.getElementById("content").innerText = allText;
                      alert(allText);
                  }
              } else {
                  alert("Can't read the file");
              }
          }
          rawFile.send(null);
    }
    readTextFile("FormulaQuestion.txt");

The FormulaQuestion.txt file is in the same directory with the html file, this approach shows an empty alert window on the browser FormulaQuestion.txt文件与 html 文件在同一目录中,这种方法在浏览器上显示一个空的警报窗口

Approach 2 using fetch method使用 fetch 方法的方法 2

fetch('FormulaQuestion.txt')
        .then(response => response.text())
        .then((data) => {
            alert(data);
        })

This doesn't show anything这没有显示任何内容

Approach 3 using JQuery使用 JQuery 的方法 3

$.get('FormulaQuestion.txt', function (data) {
        alert(data)
    }, 'text');

This doesn't work either.这也行不通。

I am building a desktop application that uses a web browser control to load html file which is embedded into the application.我正在构建一个桌面应用程序,它使用 Web 浏览器控件加载嵌入到应用程序中的 html 文件。 The application reads the string from sqlite database and save it in the FormulaQuestion.txt file, then refreshes the WebControl component which reloads the html file.应用程序从 sqlite 数据库读取字符串并将其保存在FormulaQuestion.txt文件中,然后刷新重新加载 html 文件的 WebControl 组件。 Now when the html file is reloaded, the JavaScript should read the text file and display it on alert() which once the alert is able to display the file content, i will then set it to a paragraph and remove the alert() .现在,当重新加载 html 文件时,JavaScript 应该读取文本文件并将其显示在alert() 上,一旦警报能够显示文件内容,我会将其设置为一个段落并删除alert() Please someone should help me out.请有人帮助我。

Browsers by design do not allow access to the file system for JavaScript, as allowing such access would be a serious security concern.浏览器的设计不允许访问 JavaScript 的文件系统,因为允许此类访问将是一个严重的安全问题。

To provide the FormulaQuestion.txt file to your script you will need to host the file on a server and request it via a HTTP request (like with your fetch).要将 FormulaQuestion.txt 文件提供给您的脚本,您需要将该文件托管在服务器上并通过 HTTP 请求(如您的 fetch)请求它。 The key thing here is that a server is needed to actually transmit the file over the HTTP protocol to your script.这里的关键是需要一个服务器来实际通过 HTTP 协议将文件传输到您的脚本。

If working locally, there are many options for running a local server.如果在本地工作,运行本地服务器有很多选择。

  • The npm serve module, npm 服务模块,
  • Wamp万普
  • Apache阿帕奇

You may also want to try out some free tier services like Vercel or Netlify.您可能还想尝试一些免费服务,如 Vercel 或 Netlify。 Both I believe allow you to just drag/drop a file and it will host it for you.我相信两者都允许您拖放一个文件,它会为您托管它。

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

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