简体   繁体   English

如何在不使用XMLHttpRequest()的情况下读取与index.html位于同一目录中的文本文件<input type=“file” id=“fileToLoad”>

[英]How do I read a text file located in the same directory as my index.html using XMLHttpRequest() without using <input type=“file” id=“fileToLoad”>

I have my index.html and in the same directory I have a text file that I put there and want to be able to read it. 我有我的index.html,并且在同一目录中我有一个文本文件,我放在那里,希望能够阅读它。 Everything I have tried has failed except when using the input type format. 除了使用输入类型格式外,我尝试过的所有内容都失败了。 I don't need to do a file selection. 我不需要做文件选择。 I know what the file is and where it is. 我知道文件是什么以及它在哪里。

Here is my function: 这是我的功能:

    <script type='text/javascript'>
    document.getElementById("FunText").innerHTML = 
    "The text should show here:" + readTextFile('file:///E:/My_Html/list.txt');

    function readTextFile(file)
    {
      var allText = "";
      var rawFile = new XMLHttpRequest();
      rawFile.open("GET", file, true);
      rawFile.onreadystatechange = function()
           {
            if(rawFile.readyState == 4)
               {
                if(rawFile.status == 200 || rawFile.status == 0) // status = 0
                   {
                    allText = rawFile.responseText;
                    alert("I get this:" + allText);
                   }
                }
          }
          rawFile.send(null);
      }

The variable allText always comes back empty. 变量allText总是返回空。 If I put "file:///E:/My_Html/list.txt" in the browser search box and it displays the contents of the file. 如果我在浏览器搜索框中输入“file:/// E:/My_Html/list.txt”,它会显示该文件的内容。 Does anyone see the problem? 有谁看到这个问题? I have tried rawFile.send(null), rawFile.send(), immediately after the open, at the end of the function. 我在函数结束时打开后立即尝试了rawFile.send(null),rawFile.send()。 Nothing works. 什么都行不通。 I have searched Stackoverflow, W3, and others. 我搜索过Stackoverflow,W3和其他人。 I am at a loss for ideas. 我不知所措。 Thank you for your patience and time. 感谢您的耐心和时间。

You could add the JQuery library to your project and use the $.get() function: 您可以将JQuery库添加到项目中并使用$.get()函数:

http://api.jquery.com/jquery.get/ http://api.jquery.com/jquery.get/

Example (as the file is in the same level as the index.html): 示例(因为文件与index.html处于同一级别):

<script type='text/javascript'>

    function readTextFile(file) {
        $.get(file, function(data) {
            document.getElementById("FunText").innerHTML = "The text should show here: " + data;
        });
    }

    readTextFile("list.txt");

</script>

暂无
暂无

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

相关问题 如何使用javascript读取与html位于同一目录中的本地文件 - How to read local file that in located in the same directory as html using javascript 读取文件直到index.html - Read file located up to index.html 如何使用 JavaScript 在我的 index.html 文件中嵌入 API 调用? - How do I embed an API call in my index.html file using JavaScript? 如何使用Javascript在index.html文件存储的目录中查找目录? - How to find directories within the directory the index.html file is stored within using Javascript? 如何在 index.html 文件中定义变量? (window.parentPage = true;) - How do I define a variable in my index.html file? (window.parentPage = true;) 如何在电子上加载 index.html 文件? - How do I load the index.html file on electron? 如何将外部.js文件添加到index.html? (VueJS2) - How do I add an external .js file to my index.html? (VueJS2) 如何使用js重定向到位于文件夹内的同一目录中的另一个html文件? - How to redirect to another html file in the same directory which is located inside a folder using js? 如何从index.html加载我的Aurelia应用,该索引与应用的其余部分不在同一文件夹中? - How can I load my Aurelia app from an index.html which is not located in the same folder as the rest of the application? 每当我使用 `npm start` 启动我的应用程序时,它都会将我带到 Rails 主屏幕上的 Ruby 而不是我的 index.html 文件 - Whenever I start my app using `npm start` it brings me to the Ruby on Rails Homescreen instead of my index.html file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM