简体   繁体   English

尝试在 HTML 页面加载时读取本地文件

[英]Trying to read a local file on page load in HTML

I'm creating a web application that references a local txt (or csv) file which will change every year.我正在创建一个引用本地 txt(或 csv)文件的 Web 应用程序,该文件每年都会更改。 I want to be able to load the contents of the file into a <textarea> on page startup.我希望能够在页面启动时将文件内容加载到<textarea>

Right now, I have functional code for loading the contents into a <textarea> by using the <input> element (thanks to a previous StackOverflow question in 2015).现在,我有使用<input>元素将内容加载到<textarea>的功能代码(感谢 2015 年的上一个 StackOverflow 问题)。 The file is selected and an event listener will call a function once the input state gets changed.文件被选中,一旦输入状态改变,事件侦听器将调用一个函数。 My challenge is modifying the code so that the file can be hard-coded and loaded on startup and still calling the function using an event (or changing the function to work without an event call).我的挑战是修改代码,以便文件可以被硬编码并在启动时加载,并且仍然使用事件调用函数(或更改函数以在没有事件调用的情况下工作)。

index.html索引.html

    <input type="file" id="fileinput" />
    <script type="text/javascript" src="dataLoad.js"></script>
    <script type="text/javascript">
    document.getElementById('fileinput').addEventListener('change', readSingleFile, false);
    </script>
    <textarea rows=20 id="area"></textarea>

dataLoad.js数据加载.js

function readSingleFile(evt) {
    //Retrieve the first (and only!) File from the FileList object
    var f = evt.target.files[0]; 
    if (f) {
        var r = new FileReader();
        r.onload = function(e) {
          var display = "";
          //a lot of unimportant stuff here
          document.getElementById('area').value = display;
        }

        r.readAsText(f);

    } else { 
        alert("Failed to load file");
    }
}

This works fine, but I don't want to manually select the file to load, I want to hard-code the file.这工作正常,但我不想手动选择要加载的文件,我想对文件进行硬编码。 The "a lot of unimportant stuff here" parses the data in the file so I left it out for clarity sake. “这里有很多不重要的东西”解析文件中的数据,所以为了清楚起见,我把它省略了。

You cannot due to security reasons.出于安全原因,您不能。

You don't want the websites you visit to be able to do this, do you?您不希望您访问的网站能够执行此操作,对吗?

Try This:尝试这个:

 <object width="300" height="300" type="text/plain" data="password.txt" border="0" >
</object

Assuming the <input type="file" /> has autocomplete on (which I believe it does by default), any files that were chosen/dragged onto the element should still be present on page load, so all you would need to do is manually trigger the change event that you added an eventListener for, like so:假设<input type="file" />autocomplete功能(我相信默认情况下会这样),任何被选择/拖到元素上的文件都应该在页面加载时仍然存在,所以你需要做的就是手动触发您eventListener添加了eventListenerchange事件,如下所示:

<input type="file" id="fileinput" />
<textarea rows=20 id="area"></textarea>

<script>
    var fileInput = document.getElementById('fileinput');
    var event     = new Event('change');

    fileInput.addEventListener('change', readSingleFile, false);

    fileInput.dispatchEvent(event);

    function readSingleFile(evt) {
        var f = evt.target.files[0];

        if (f) {
            alert("Processing file...");
        }
    }
</script>

This won't work in JSFiddle as it rebuilds the HTML when you refresh the page, however this works offline for me.这在 JSFiddle 中不起作用,因为它会在您刷新页面时重建 HTML,但是这对我来说是离线工作的。

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

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