JS FileReader:从本地文件和CSV文件中读取CSV jQuery的CSV [英] JS FileReader: Read CSV from Local File & jquery-csv
问题描述
我在与html页面相同的目录中有一个CSV文件,并且我想使用FileReader将文件内容读入到jquery-csv的To Arrays函数中,但是我似乎无法使其正常工作适当地.我想我了解此任务的异步性,但是我正确地描述了它吗?在这里,我试图在第二列中输出第二个单元格.感谢您的帮助.
I have a CSV file in the same directory as an html page, and I'd like to use FileReader to read the file contents into a jquery-csv's To Arrays function, but I can't seem to get it to work properly. I think I understand the asynchrony of this task, but have i depicted it correctly? Here, I'm trying to output the 2nd cell in the 2nd column. Thanks for any help.
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['timeline']}]}"></script>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="jquery.csv-0.71.js"></script>
<script type="text/javascript">
var reader = new FileReader();
reader.onload = function(e) {
var text = e.target.result;
var data = $.csv.toArrays(text);
document.write(data[1][1]);
}
reader.readAsText('data.csv');
</script>
推荐答案
它不起作用.您无权从浏览器中读取带有javascript的文件.处理它的唯一方法是创建一个input [type = file]标记并向其添加onChange事件.例如:
It's not going to work.You have no permissions to read files with javascript from the browser. The only way to deal with it is to create an input[type=file] tag and add onChange event to it. For example:
document.getElementById('fileupload').addEventListener('change', function (e) {
var files = e.target.files;
//proceed your files here
reader.readAsText(files[0]);
}, false);
这篇关于JS FileReader:从本地文件和CSV文件中读取CSV jQuery的CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!