简体   繁体   English

使用javascript显示txt文件中的文本

[英]To display text from a txt file using javascript

How to read text from a txt file with one button to browse the file and other button to display text. 如何使用一个按钮浏览txt文件并使用另一个按钮显示文本来从txt文件读取文本。 Pls help me in getting the code. 请帮助我获取代码。 i have tried many codes but othing worked. 我已经尝试了许多代码,但是其他都可以。 some code was like this. 一些代码是这样的。 Thanks in advance 提前致谢

<!DOCTYPE html>
<html>
  <head>
    <title>reading file</title>
    <script type="text/javascript">

        var reader = new FileReader();

        function readText(that){

            if(that.files && that.files[0]){
                var reader = new FileReader();
                reader.onload = function (e) {  
                    var output=e.target.result;
                    //process text to show only lines with "@":             
            output=output.split("\n").filter(/./.test, /\@/).join("\n");

                document.getElementById('main').innerHTML= output;
                };//end onload()
                reader.readAsText(that.files[0]);
            }//end if html5 filelist support
        } 
</script>
</head>
<body>
    <input type="file" onchange='readText(this)' />
    <div id="main"></div>
  </body>
</html>

You should properly read an article like this: http://www.html5rocks.com/en/tutorials/file/dndfiles/ 您应该正确阅读如下文章: http : //www.html5rocks.com/en/tutorials/file/dndfiles/

Dont think this line is working properly: 不要以为这条线工作正常:

output=output.split("\n").filter(/./.test, /\@/).join("\n");

Try changing it to: 尝试将其更改为:

output=output.split("\n").filter(function(l) {
    //return /^\@/.test(l); // Starting with @
    return l.indexOf('@') > -1; // Containing @
}).join("\n");

It would be interesting to see if this would work as well: 看看这是否也将很有趣:

output=output.split("\n").filter(/\@/.test.bind(/\@/)).join("\n");

The second arguments passed to the .filter method is the context: 传递给.filter方法的第二个参数是上下文:

array.filter(callback[, thisObject]) array.filter(callback [,thisObject])

Get the input file, use FileReader() to read it,on file load get text that matches the pattern. 获取输入文件,使用FileReader()读取它,在文件加载时获取与模式匹配的文本。 Finally display it through "main" div. 最后通过“ main” div显示它。 Should work.. 应该管用..

HTML : HTML:

<input id="fileInput" type="file"/>
<div id="main"></div>

jQuery : jQuery的:

$(function(){
        $("#fileInput").change(function(e){
            var myFile = e.target.files[0];
            var reader = new FileReader();
            reader.onload = function(e){
                var output = e.target.result;
                output=output.split("\n").filter(/./.test, /\@/).join("\n");
                $("#main").text(output);
            };
            reader.readAsText(myFile)
        });
    }
)

Demo 演示版

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

相关问题 使用javascript从txt文件读取文本,并在引导模式对话框中显示(通过组合键绑定) - Read text from a txt file using javascript and display in a bootstrap modal dialog (via knockout binding) 使用JavaScript将文本从本地.txt文件加载到html textarea - Load text from local .txt file into html textarea using JavaScript 将.txt文件中的文本放入 <p> 使用JavaScript的标签 - Put text from a .txt file into <p> tags using JavaScript 从 .txt 文件加载文本并在 jsPsychHtmlButtonResponse 中显示 - Load text from a .txt file and display in jsPsychHtmlButtonResponse 使用Javascript动态读取和显示txt文件 - Using Javascript to read and display a txt file dynamically 将 javascript 变量中的文本保存到 .txt 文件 - Save text from javascript variable to .txt file 使用JavaScript从URL提取文本并显示在php文件上 - Extract text from URL using javascript and display on php file 如何使用 javascript 从元素中获取文本并将其保存到新行 in.txt 文件中? - How to get text from element and save it to a new line in .txt file using javascript? 如何使用Java脚本删除文本(.txt)文件中的最后一行 - How to remove last line in the text(.txt) file using Javascript JavaScript从Google驱动器或Dropbox上的txt文件读取所有文本 - JavaScript read all text from txt file on Google drive or Dropbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM