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
4 Answers
Reset to default 1you 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/
- 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
- In your client code
< input type='file' size='25' name='fileToUpload'>
<button onclick='return ajaxFileUpload();'>Ajax Read</button>
- 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
版权声明:本文标题:jquery - how to create an array by reading text file in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742400220a2467736.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论