How can I parse a text file using javascript

前端 未结 3 699
北荒
北荒 2020-12-09 11:40

The code below is to read a text file using javascript. it works. However, I just want to read part of the content. For example, the content of the file is :\"Hello world!\

相关标签:
3条回答
  • 2020-12-09 12:14

    Try this to read separate words if I understood correctly what you need. Create a file with the contents "hello world" and browse to it with the example script. The output is "hello".

    <html>
    <head>
    <input type="file" id="fileinput" />
    <script type="text/javascript">
      function readSingleFile(evt) {
        var f = evt.target.files[0];   
        if (f) {
          var r = new FileReader();
          r.onload = function(e) { 
              var contents = e.target.result;             
              var ct = r.result;
              var words = ct.split(' ');            
              alert(words[0]);
          }
          r.readAsText(f);
        } else { 
          alert("Failed to load file");
        }
      }
    
      document.getElementById('fileinput').addEventListener('change', readSingleFile, false);
    </script>
    </head>
    <body>
    </body>
    </html>
    

    Reading directly has to be with an ajax request due to the javascript restrictions regarding safety. This code shoudl perform the requested operation:

    <html>
    <head>
    <input type="file" id="fileinput" />
    <script type="text/javascript">
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function(){
      if(xmlhttp.status==200 && xmlhttp.readyState==4){    
        var words = xmlhttp.responseText.split(' ');
        alert(words[0]);
      }
    }
    xmlhttp.open("GET","FileName.txt",true);
    xmlhttp.send();
    </script>
    </head>
    <body>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-09 12:25

    Opening a file in javascript with ajax (without using any framework)

    var urls = ["data.txt"];
    xhrDoc= new XMLHttpRequest();   
    xhrDoc.open('GET', urls[0] , async)
    if (xhrDoc.overrideMimeType)
        xhrDoc.overrideMimeType('text/plain; charset=x-user-defined')
    xhrDoc.onreadystatechange =function()
    {
    if (this.readyState == 4)
    {
        if (this.status == 200)
       {
        var data= this.response; //Here is a string of the text data 
       }
    
    }                   
    }
    xhrDoc.send() //sending the request
    
    0 讨论(0)
  • 2020-12-09 12:26

    I used

    jQuery.get('http://localhost/foo.txt', function(data) {
    var myvar = data;
    });
    

    , and got data from my text file.

    Or try this

    JQuery provides a method $.get which can capture the data from a URL. So to "read" the html/text document, it needs to be accessible through a URL. Once you fetch the HTML contents you should just be able to wrap that markup as a jQuery wrapped set and search it as normal.

    Untested, but the general gist of it...

    var HTML_FILE_URL = '/whatever/html/file.html';
    
    $(document).ready(function() {
        $.get(HTML_FILE_URL, function(data) {
            var fileDom = $(data);
            fileDom.find('h2').each(function() {
                alert($(this).text());
            });
        });
    });
    
    0 讨论(0)
提交回复
热议问题