js

JS Papaparse Read CSV

Posted by ExiaHuang on August 1, 2017

JS Read CSV

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<html>
<meta charset="utf-8"/>
  <head>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <script type="text/javascript" src="./papaparse.min.js"></script>

  </head>
  <body>

<script>
  var data;
 
  function handleFileSelect(evt) {
    var file = evt.target.files[0];
 
    Papa.parse(file, {
      header: true,
      encoding: 'Shift-JIS',
      dynamicTyping: true,
      skipEmptyLines: true,
      complete: function(results) {
        data = results;
        console.log(data);
      }
    });
  }
 
  $(document).ready(function(){
    $("#csv-file").change(handleFileSelect);
  });
</script>
<input type="file" id="csv-file" name="files"/>

  </body>
</html>