-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
66 lines (46 loc) · 1.84 KB
/
index.html
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Read Text File</title>
</head>
<body>
<input type="file" name="inputfile" id="inputfile">
<br>
<div id="output"></div>
<script type="text/javascript">
document.getElementById('inputfile')
.addEventListener('change', function () {
var fr = new FileReader();
fr.onload = function () {
// document.getElementById('output')
// .textContent = fr.result;
document.getElementById('output').innerHTML = csvJSON(fr.result)
console.log(csvJSON(fr.result))
}
fr.readAsText(this.files[0], 'ISO-8859-1');
})
//var csv is the CSV file with headers
function csvJSON(csv) {
var lines = csv.split("\n");
var result = [];
// NOTE: If your columns contain commas in their values, you'll need
// to deal with those before doing the next step
// (you might convert them to &&& or something, then covert them back later)
// jsfiddle showing the issue https://jsfiddle.net/
var headers = lines[0].split(",");
console.log(headers)
for (var i = 1; i < lines.length; i++) {
var obj = {};
var currentline = lines[i].split(",");
for (var j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
// return result; //JavaScript object
return JSON.stringify(result); //JSON
}
</script>
</body>
</html>