-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
153 lines (140 loc) · 4.52 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Javascript CSV parser with HTML5 File Reader</title>
<style type="text/css">
body {
font: 12px/20px "Lucida Grande", Tahoma, Verdana, sans-serif;
color: #404040;
background: #47496D;
background-image: -webkit-radial-gradient(center, circle cover, #6b6e8c, #212649);
background-image: -moz-radial-gradient(center, circle cover, #6b6e8c, #212649);
background-image: -o-radial-gradient(center, circle cover, #6b6e8c, #212649);
background-image: radial-gradient(center, circle cover, #6b6e8c, #212649);
}
#wrap{
margin: 80px auto;
width: 960px;
padding: 10px;
overflow: hidden;
background: #FAFAFA;
border-radius: 4px;
-webkit-box-shadow: 0 0 4px rgba(0, 0, 0, 0.4);
box-shadow: 0 0 4px rgba(0, 0, 0, 0.4);
}
form{
background: #CCC;
border-radius: 2px;
padding: 10px;
}
table {
max-width: 100%;
background-color: #fff;
border-collapse: collapse;
border-spacing: 0;
width: 100%;
margin-bottom: 20px;
}
table th{
background: #eae4ff;
}
table th,
table td {
padding: 8px;
line-height: 20px;
text-align: left;
vertical-align: top;
border-top: 1px solid #dddddd;
}
table {
border: 1px solid #dddddd;
border-collapse: separate;
*border-collapse: collapse;
border-left: 0;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
table th,
table td {
border-left: 1px solid #dddddd;
}
table tr:nth-child(odd){
background-color: #f9f9f9;
}
table tr:hover td
{
background-color: #f5f5f5;
}
table tr:first-child{
font-weight: bold;
}
</style>
</head>
<body>
<div id="wrap">
<h1>HTML5 and Javascript CSV Parser</h1>
<form action="javascript:void(0);" id="the_form">
<input type="file" id="the_file" required="required" accept=".csv"/>
<input type="submit" value="Go" class="btn"/>
</form>
<div id="file_info"></div>
<div id="list"></div>
</div>
<script type="text/javascript">
function fileInfo(e){
var file = e.target.files[0];
if (file.name.split(".")[1].toUpperCase() != "CSV"){
alert('Invalid csv file !');
e.target.parentNode.reset();
return;
}else{
document.getElementById('file_info').innerHTML = "<p>File Name: "+file.name + " | "+file.size+" Bytes.</p>";
}
}
function handleFileSelect(){
var file = document.getElementById("the_file").files[0];
var reader = new FileReader();
var link_reg = /(http:\/\/|https:\/\/)/i;
reader.onload = function(file) {
var content = file.target.result;
var rows = file.target.result.split(/[\r\n|\n]+/);
var table = document.createElement('table');
for (var i = 0; i < rows.length; i++){
var tr = document.createElement('tr');
var arr = rows[i].split(',');
for (var j = 0; j < arr.length; j++){
if (i==0)
var td = document.createElement('th');
else
var td = document.createElement('td');
if( link_reg.test(arr[j]) ){
var a = document.createElement('a');
a.href = arr[j];
a.target = "_blank";
a.innerHTML = arr[j];
td.appendChild(a);
}else{
td.innerHTML = arr[j];
}
tr.appendChild(td);
}
table.appendChild(tr);
}
document.getElementById('list').appendChild(table);
};
reader.readAsText(file);
}
document.getElementById('the_form').addEventListener('submit', handleFileSelect, false);
document.getElementById('the_file').addEventListener('change', fileInfo, false);
</script>
<!-- LOVE SOURCE -->
<!-- .------..------..------..------..------. .------..------..------.
|R.--. ||O.--. ||H.--. ||I.--. ||T.--. |.-. |R.--. ||O.--. ||X.--. |
| :(): || :/\: || :/\: || (\/) || :/\: ((5)) | :(): || :/\: || :/\: |
| ()() || :\/: || (__) || :\/: || (__) |'-.-.| ()() || :\/: || (__) |
| '--'R|| '--'O|| '--'H|| '--'I|| '--'T| ((1)) '--'R|| '--'O|| '--'X|
`------'`------'`------'`------'`------' '-'`------'`------'`------' -->
</body>
</html>