-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileupload.js
More file actions
137 lines (102 loc) · 3.49 KB
/
fileupload.js
File metadata and controls
137 lines (102 loc) · 3.49 KB
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
var express = require('express');
var path = require('path');
var http = require('http');
var formidable = require('formidable');
const parse = require('csv-parse')
const app =express();
const mysql = require('mysql');
var xlsx = require('node-xlsx').default;
/* const xlsx= require('xlsx');
const workbook = xlsx.readFile('.//files//Financial_Sample.xlsx');
const sheet_name_list = workbook.SheetNames;
console.log(xlsx.utils.sheet_to_json(workbook.Sheets[sheet_name_list]));
*/
var fs = require('fs');
/*
const db =mysql.createConnection({
host: 'localhost',
user :'root',
password:'',
database:'test'
});
// establishing the connection
db.connect((err)=>{
if(err){
throw err;
}
console.log('connection established..');
}); */
/* var data = fs.readFileSync('.//files//Financial_Sample.xlsx', {
encoding: 'ascii'
});
var json = JSON.parse(data);
for (var list in json) {
var devices = json[list];
for (var i = 0; i < devices.length; i++) {
var device = devices[i];
if (device["uid"])
// console.log("UPDATE panel.device SET hardware_id = '" + device["devid"] + "' WHERE uid = '" + device["uid"] + "' AND project = 4;");
console.log("INSERT INTO excel(Segment, Country, Product) \
VALUES('?', '?', '?')")
}
}
*/
/*
app.post('/ping', function (req, res) {
res.send(res.body);
var jsondata = JSON.stringify('.//files//data.json'); */
// var test = JSON.parse(jsondata);
//let jsondata = JSON.stringify(json);
//console.log(typeof jsondata); // string
/*
let datajson = JSON.parse(jsondata);
console.log(typeof datajson); // object
console.log(datajson.GradeA); // 25
db.query("INSERT INTO json(GradeA, GradeB, GradeC) VALUES('?','?','?')", [datajson], function(err, result){
if (err) throw err;
//if no error, resulst return as follow
console.log(result);
console.log("Number of rows affected :" + result.affectedRows);
console.log("Number of records affected with warning : " + result.warningCount);
console.log("Message from MySQL Server : " + result.message);
});
});
*/
/*
let counter= 0;
var contents = fs.readFile(".//files//data.json", function(err,data){
if (err) throw err;
console.log(Object.keys(JSON.parse(data)));
});
app.listen('3000',()=>{
console.log('server started on port 3000');
}); */
// html file containing upload form
var upload_html = fs.readFileSync("upload.html");
// replace this with the location to save uploaded files
var upload_path = "C:/Users/Nour.A/Documents/";
http.createServer(function (req, res) {
if (req.url == '/uploadform') {
res.writeHead(200);
res.write(upload_html);
return res.end();
} else if (req.url == '/fileupload') {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
// oldpath : temporary folder to which file is saved to
var oldpath = files.filetoupload.path;
var newpath = upload_path + files.filetoupload.name;
// copy the file to a new location
fs.rename(oldpath, newpath, function (err) {
if (err) throw err;
// you may respond with another html page
res.write('File uploaded and moved!');
res.end();
});
});
}
}).listen(3000);
/* app.get('/download', function(req, res){
var file = './/files/data.json';
res.download(file);
}); */