开发者

how to create an array by reading text file in javascript

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 the array. if someone know how to do this please reply me as soon as possible. Note : I have to stored this file anywhere开发者_开发知识库 it's my choice. The only thing I have to do is, read the text file by just giving complete path of file. It is not necessary the solution should be in javascript you can reply with jquery or ajax solution. thank you.


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/


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


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


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(',')


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜