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
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/
def upload_file data = params['fileToUpload'].read render :json => data.split(',') end
< input type='file' size='25' name='fileToUpload'>
<button onclick='return ajaxFileUpload();'>Ajax Read</button>
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.
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/