简体   繁体   English

使用javascript FileReader从本地页面打开本地文件

[英]Open a local file from a local page using javascript FileReader

Is it possible to open a local file from a local page using FileReader without the < input type=file id=files> The file is in the same directory as the page on a local machine. 是否可以使用FileReader从本地页面打开本地文件而不使用< input type=file id=files>该文件与本地计算机上的页面位于同一目录中。

reader = new FileReader();
reader.readAsText("output_log.txt");

I have created a sample code using Jquery/javascript which will read a local text file and display its content in Html page 我使用Jquery / javascript创建了一个示例代码,它将读取本地文本文件并在HTML页面中显示其内容

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){

$("#fileinput").on('change',function(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 contents = e.target.result;

        $("#filename").html(f.name);
        $("#fileType").html(f.type);
        $("#display_file_content").html(contents);
      }
      r.readAsText(f);
    } else { 
      alert("Failed to load file");
    } 

});

});

</script>
</head>

<body>
<label>Please upLoad a file</label>
<input type="file" id="fileinput" />
</br>
<div>
<b>File Name:-</b><label id="filename"></label> </br>
<b>File Type:-</b><label id="fileType"></label></br>
<b>Content<b/>
<pre id="display_file_content">
</pre>
<div>
</body>
</html>

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

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