javascript 如何通过在javascript中读取文本文件来创建数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5846556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to create an array by reading text file in javascript
提问by Lokesh Paunikar
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.
我有一个文本文件,其中包含用逗号 (,) 分隔的脏话。现在我必须通过读取这个文本文件来创建一个数组,每个脏词都应该存储到数组中。如果有人知道如何做到这一点,请尽快回复我。注意:我必须将此文件存储在我选择的任何位置。我唯一要做的就是通过提供文件的完整路径来读取文本文件。解决方案不必在 javascript 中,您可以使用 jquery 或 ajax 解决方案进行回复。谢谢。
回答by kobe
you can't do IO operations from Javascript,
你不能从 Javascript 进行 IO 操作,
you can probably try ajax
你可以试试ajax
http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/jQuery.ajax/
or jquery load
或 jquery 加载
回答by Nicky Waites
Here is a jQuery + AJAX Solution that you can use
这是您可以使用的 jQuery + AJAX 解决方案
//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
在来自http://en.wikipedia.org/wiki/Comma-separated_values 的CSV 上进行测试
You could hide the file input control by using something like this http://plugins.jquery.com/project/custom-file
您可以使用这样的东西来隐藏文件输入控件http://plugins.jquery.com/project/custom-file
回答by samccone
see this resource and code, this is a really great post about this issue
看到这个资源和代码,这是一个关于这个问题的非常棒的帖子
回答by Jirapong
Technically you can do by- http://www.phpletter.com/Our-Projects/AjaxFileUpload/
从技术上讲,您可以通过- http://www.phpletter.com/Our-Projects/AjaxFileUpload/
- Upload file to your server side code, parse it back with json array
- 将文件上传到您的服务器端代码,使用 json 数组解析它
def upload_file data = params['fileToUpload'].read render :json => data.split(',') end
- In your client code
- 在您的客户端代码中
< input type='file' size='25' name='fileToUpload'>
< input type='file' size='25' name='fileToUpload'>
<button onclick='return ajaxFileUpload();'>Ajax Read</button>
<button onclick='return ajaxFileUpload();'>Ajax Read</button>
- In your ajaxFileUpload() method to handle the return data.split(',')
- 在你的 ajaxFileUpload() 方法中处理返回的 data.split(',')
回答by ggutenberg
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.
您能否在阅读之前手动操作 CSV 文件?我有一个类似的问题,但能够让生成它的人对其做一些事情,例如对'
→\'
进行全局搜索/替换,然后将内容包装在 JS 字符串变量中。然后我就像包含任何其他 JS 文件一样包含新文件。
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:
原始 CSV:
word,"multiple words",apostrophe's
Modified JS CSV:
修改后的 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.
然后我使用 samccone 发布的 Ben Nadel 链接进行实际解析。