Tôi có một tệp CSV trong cùng thư mục với trang HTML và tôi muốn sử dụng Filereader để đọc nội dung tệp vào hàm jQuery-CSV để mảng, nhưng dường như tôi không thể hoạt động chính xác. Tôi nghĩ rằng tôi hiểu sự không đồng bộ của nhiệm vụ này, nhưng tôi đã mô tả nó một cách chính xác chưa? Ở đây, tôi đang cố gắng xuất ra ô thứ 2 trong cột thứ 2. Cảm ơn vì bất kì sự giúp đỡ.
<script type="text/javascript" src="//www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['timeline']}]}"></script> <script src="//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>
Evan Plaice
Huy hiệu vàng 13,9k674 Huy hiệu bạc94 Huy hiệu đồng6 gold badges74 silver badges94 bronze badges
Khi được hỏi ngày 13 tháng 5 năm 2015 lúc 19:10May 13, 2015 at 19:10
1
Đây là bản demo hoạt động được cung cấp bởi JQuery-CSV
<html> <head> <meta charset="utf-8" /> <title>Demo - CSV-to-Table</title> </head> <body> <div id="inputs" class="clearfix"> <input type="file" id="files" name="files[]" multiple /> </div> <hr /> <output id="list"> </output> <hr /> <table id="contents" style="width:100%; height:400px;" border> </table> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script src="//evanplaice.github.io/jquery-csv/src/jquery.csv.min.js"></script> <script> $(document).ready(function() { if(isAPIAvailable()) { $('#files').bind('change', handleFileSelect); } }); function isAPIAvailable() { // Check for the various File API support. if (window.File && window.FileReader && window.FileList && window.Blob) { // Great success! All the File APIs are supported. return true; } else { // source: File API availability - //caniuse.com/#feat=fileapi // source: <output> availability - //html5doctor.com/the-output-element/ document.writeln('The HTML5 APIs used in this form are only available in the following browsers:<br />'); // 6.0 File API & 13.0 <output> document.writeln(' - Google Chrome: 13.0 or later<br />'); // 3.6 File API & 6.0 <output> document.writeln(' - Mozilla Firefox: 6.0 or later<br />'); // 10.0 File API & 10.0 <output> document.writeln(' - Internet Explorer: Not supported (partial support expected in 10.0)<br />'); // ? File API & 5.1 <output> document.writeln(' - Safari: Not supported<br />'); // ? File API & 9.2 <output> document.writeln(' - Opera: Not supported'); return false; } } function handleFileSelect(evt) { var files = evt.target.files; // FileList object var file = files[0]; // read the file metadata var output = '' output += '<span style="font-weight:bold;">' + escape(file.name) + '</span><br />\n'; output += ' - FileType: ' + (file.type || 'n/a') + '<br />\n'; output += ' - FileSize: ' + file.size + ' bytes<br />\n'; output += ' - LastModified: ' + (file.lastModifiedDate ? file.lastModifiedDate.toLocaleDateString() : 'n/a') + '<br />\n'; // read the file contents printTable(file); // post the results $('#list').append(output); } function printTable(file) { var reader = new FileReader(); reader.readAsText(file); reader.onload = function(event){ var csv = event.target.result; var data = $.csv.toArrays(csv); var html = ''; for(var row in data) { html += '<tr>\r\n'; for(var item in data[row]) { html += '<td>' + data[row][item] + '</td>\r\n'; } html += '</tr>\r\n'; } $('#contents').html(html); }; reader.onerror = function(){ alert('Unable to read ' + file.fileName); }; } </script> </body> </html>
Tuyên bố miễn trừ trách nhiệm: Tôi là tác giả của JQuery-CSV
Đã trả lời ngày 21 tháng 1 năm 2016 lúc 11:54Jan 21, 2016 at 11:54
Evan Plaitevan PlaiceEvan Plaice
Huy hiệu vàng 13,9k674 Huy hiệu bạc94 Huy hiệu đồng6 gold badges74 silver badges94 bronze badges
5
Khi được hỏi ngày 13 tháng 5 năm 2015 lúc 19:10
document.getElementById('fileupload').addEventListener('change', function (e) { var files = e.target.files; //proceed your files here reader.readAsText(files[0]); }, false);Đây là bản demo hoạt động được cung cấp bởi JQuery-CSVMay 13, 2015 at 19:34
Tuyên bố miễn trừ trách nhiệm: Tôi là tác giả của JQuery-CSVAnton Savchenko
Đã trả lời ngày 21 tháng 1 năm 2016 lúc 11:541 gold badge7 silver badges11 bronze badges
0