admin管理员组

文章数量:1335840

I have a text file with profanity word separated by ma (,). 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 plete path of file. It is not necessary the solution should be in javascript you can reply with jquery or ajax solution. thank you.

I have a text file with profanity word separated by ma (,). 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 plete path of file. It is not necessary the solution should be in javascript you can reply with jquery or ajax solution. thank you.

Share Improve this question edited May 1, 2011 at 5:14 Lokesh Paunikar asked May 1, 2011 at 4:57 Lokesh PaunikarLokesh Paunikar 1,7195 gold badges21 silver badges26 bronze badges 1
  • Are you using ajax to load the file? – qwertymk Commented May 1, 2011 at 5:04
Add a ment  | 

4 Answers 4

Reset to default 1

you can't do IO operations from Javascript,

you can probably try ajax

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

or jquery load

http://api.jquery./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/wiki/Comma-separated_values

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

Technically you can do by - http://www.phpletter./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.

本文标签: jqueryhow to create an array by reading text file in javascriptStack Overflow