forked from tharakanandakumara/sign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exceluploader.php
238 lines (201 loc) · 7.93 KB
/
exceluploader.php
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
session_start();
include 'logout.php';
//$_SESSION['token']=null;
if(!isset($_SESSION['token'])){
header("location: login.html");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link href="css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<link href="style.css" rel="stylesheet" id="bootstrap-css">
<script src="js/jquerymin.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/noty.min.js"></script>
<script type="text/javascript" src="js/jquery.serializejson.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="container register">
<div class="row">
<div class="col-md-3 register-left">
<img src="src/Logo.jpg" alt="" />
<h3>Welcome</h3>
<p>Joseph Vaz College - Student Registration</p>
<button id="checkUser" type="button" class="btn btn-primary" data-toggle="modal" data-target="#basicExampleModal">
Check Student
</button>
<button id="updateUser" type="button" class="btn btn-primary" data-toggle="modal" data-target="#basicUpdateeModal">
Update Student
</button>
<button id="updateUser" type="button" class="btn btn-primary" data-toggle="modal" data-target="#basicUpdateeModal">
Upload Excel
</button>
<br>
<a id="logoutBtn" href="index.php?logout=1">Logout</a>
</div>
<div class="col-md-9 register-right">
<div class="notify_panel"></div>
<form id="uploader" caction="upload-manager.php" method="post" enctype="multipart/form-data">
<h2>Upload File</h2>
<label for="fileSelect">Filename:</label>
<input type="file" name="photo" id="fileSelect" class="form-control">
<div class="form-group">
<button id="uploadBtn" type="submit" class="btn btn-primary login-btn btn-block">Upload</button>
</div>
<p><strong>Note:</strong> Only .xlsx file format allowed to a max size of 5 MB.</p>
</form>
</div>
</div>
</div>
<script src="frontend/properties.js"></script>
<script>
var web_token = "<?php echo $_SESSION['token'] ?>";
var auth = "BEARER " + web_token;
$('#uploader').submit(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "upload-manager.php",
contentType: false,
cache: false,
processData: false,
data: new FormData(this),
// Update Url
success: function(response) { // Setting Token
if (response) {
var code = response.split(":");
console.log("Code : " + code[0]);
if (code[0] == "S1200 ") {
console.log(response);
xltojson(code[2]);
} else {
console.log(response);
//error notification here
}
} else {
// notifyMe('.notify_panel', 'Invalid Credentials Entered', '0');
}
},
statusCode: {
404: function() {
//notifyMe('.notify_panel', 'Invalid Username', '0');
},
401: function() {
//notifyMe('.notify_panel', 'Invalid password', '0');
}
}
});
//console.log("token : " + token);
return false;
});
function xltojson() {
var url = gOptions.clientUrl+"/upload/test.xlsx";
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";
oReq.onload = function(e) {
var arraybuffer = oReq.response;
/* convert data to binary string */
var data = new Uint8Array(arraybuffer);
var arr = new Array();
for (var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
var bstr = arr.join("");
/* Call XLSX */
var workbook = XLSX.read(bstr, {
type: "binary",
cellDates: true, cellNF: false, cellText: false
});
/* DO SOMETHING WITH workbook HERE */
var first_sheet_name = workbook.SheetNames[0];
/* Get worksheet */
var worksheet = workbook.Sheets[first_sheet_name];
var json = XLSX.utils.sheet_to_json(worksheet, {
raw: false,
dateNF: 'YYYY-MM-DD'
})
//json="{students:"+json+"}";
console.log(JSON.stringify({
'students': json
}));
dataPoster(JSON.stringify({
'students': json
}));
}
oReq.send();
}
function dataPoster(datatoPost) {
$.ajax({
type: "POST",
url: gOptions.serverUrl+"/protected/students/multiple",
dataType: 'json',
data: datatoPost,
contentType: 'application/json;charset=UTF-8',
// Update Url
headers: {
Authorization: auth
},
success: function(response) { // Setting Token
if (response) {
notifyMe('.notify_panel', response.message, '1');
deleteFile();
} else {
notifyMe('.notify_panel', 'Invalid Credentials Entered', '0');
}
},
statusCode: {
400: function(response) {
notifyMe('.notify_panel', response.responseJSON.message, '0');
deleteFile()
},
401: function(response) {
notifyMe('.notify_panel', response.responseJSON.message, '0');
deleteFile()
},
500: function(response) {
notifyMe('.notify_panel', response.responseJSON.message, '0');
deleteFile()
}
}
});
}
function deleteFile() {
$.ajax({
type: "POST",
url: "deletefile.php",
// Update Url
success: function(response) { // Setting Token
console.log("deleted file")
},
error: function(err) {
console.log("delete unsuccess")
}
});
}
function notifyMe($classname, $message, $status) {
if ($status == "1") {
statusnew = 'success';
} else {
statusnew = 'error';
}
var n = $($classname).noty({
text: $message,
theme: 'metroui',
type: statusnew,
animation: {
open: {
height: 'toggle'
},
close: {
height: 'toggle'
},
speed: 500
}
});
n.setTimeout(3000);
}
</script>
<script src="js/notify.js"></script>
<script src="js/xl-min.js"></script>
</head>
</html>