how to create an array by reading text file in javascript

后端 未结 5 1086
日久生厌
日久生厌 2020-12-22 08:35

I have a text file with profanity word separated by comma (,). Now I have to create an array by reading this text file, each and every profanity word should be stored into t

相关标签:
5条回答
  • 2020-12-22 09:04

    Here is a jQuery + AJAX Solution that you can use

    //HTML
    <input id="file" type="file" />
    <input type="button" id="load" value="Load CSV" />
    
    //JavaScript/jQuery
    $(function () {
    
      //Stop caching if csv is likely to change often (change $.get to $.ajax)
      $.ajaxSetup({
         cache: false
      });
    
      $('#load').click(function () {
    
         $.get($('#file').val(), function (content) {
            var output = content.split(new RegExp(",|\r"))
                                .map(function (element) {
               //Do what ever tidy up here
               return $.trim(element).toLowerCase();
            });
            console.log(output);
         });
    
      });
    
    });
    

    Tested on CSV from http://en.wikipedia.org/wiki/Comma-separated_values

    You could hide the file input control by using something like this http://plugins.jquery.com/project/custom-file

    0 讨论(0)
  • 2020-12-22 09:09

    see this resource and code, this is a really great post about this issue

    http://www.bennadel.com/blog/1504-Ask-Ben-Parsing-CSV-Strings-With-Javascript-Exec-Regular-Expression-Command.htm

    0 讨论(0)
  • 2020-12-22 09:14

    Technically you can do by - http://www.phpletter.com/Our-Projects/AjaxFileUpload/

    1. Upload file to your server side code, parse it back with json array
        def upload_file    
           data = params['fileToUpload'].read
           render :json => data.split(',')
        end
    
    1. In your client code

    < input type='file' size='25' name='fileToUpload'>

     <button onclick='return ajaxFileUpload();'>Ajax Read</button>
    

    1. In your ajaxFileUpload() method to handle the return data.split(',')
    0 讨论(0)
  • 2020-12-22 09:26

    Are you able to manipulate the CSV file manually before reading it? I had a similar issue, but was able to get the person that generated it to do a few things to it such as doing a global search/replace for '\' then wrapping the contents in a JS string variable. Then I just included the new file as I would any other JS file.

    Ex.

    <!-- Modified CSV file with single JS variable -->
    <script type="text/javascript" src="csvFile.js"></script>
    <!-- Ben Nadel CSV Parser and any other custom code -->
    <script type="text/javascript" src="parseCsv.js"></script>
    

    Original CSV:

    word,"multiple words",apostrophe's
    

    Modified JS CSV:

    var csvData = 'word,"multiple words",apostrophe\'s';
    

    And then I used the Ben Nadel link posted by samccone to do the actual parsing.

    0 讨论(0)
  • 2020-12-22 09:27

    you can't do IO operations from Javascript,

    you can probably try ajax

    http://api.jquery.com/jQuery.ajax/

    or jquery load

    http://api.jquery.com/load/

    0 讨论(0)
提交回复
热议问题