forked from mick26/ng_Node-AdvancedFileUpload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
148 lines (110 loc) · 4.83 KB
/
server.js
File metadata and controls
148 lines (110 loc) · 4.83 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
138
139
140
141
142
143
144
145
146
/*=========================================================
Michael Cullen
File Upload
server.js
May 2014
Working - (Tá se ag obair)
============================================================*/
/* ==========================================================
External Modules/Packages Required
============================================================ */
var express = require('express'); //Express Web Server
var busboy = require('connect-busboy'); //middleware for form/file upload
//var im = require('imagemagick');
//var easyimg = require('easyimage'); //image processing
var path = require('path'); //used for file path
var fs = require('fs-extra'); //File System - for file manipulation
var util = require('util'); //for stream
/* ==========================================================
Create a new application with Express
============================================================ */
var app = express();
/* ==========================================================
Use busboy middleware
============================================================ */
app.use(busboy());
/* ==========================================================
serve the static index.html from the public folder
============================================================ */
app.use(express.static(path.join(__dirname, 'public')));
/* ==========================================================
Create a Route (/upload) to handle the Form submission
(handle POST & PUT requests to /upload)
Express v4 Route definition
============================================================ */
app.route('/upload')
.post(function (req, res, next) {
var arr;
var fstream;
var filesize = 0;
req.pipe(req.busboy);
//--------------------------------------------------------------------------
req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
//uploaded file name, encoding, MIME type
console.log('File [' + fieldname +']: filename:' + filename + ', encoding:' + encoding + ', MIME type:'+ mimetype);
//uploaded file size
file.on('data', function(data) {
console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
fileSize = data.length;
console.log("fileSize= " + fileSize);
});
file.on('end', function() {
console.log('File [' + fieldname + '] ENDed');
console.log("-------------------------");
});
//populate array
//I am collecting file info in data read about the file. It may be more correct to read
//file data after the file has been saved to img folder i.e. after file.pipe(stream) completes
//the file size can be got using stats.size as shown below
arr= [{fieldname: fieldname, filename: filename, encoding: encoding, MIMEtype: mimetype}];
//Path where image will be uploaded
fstream = fs.createWriteStream(__dirname + '/img/' + filename); //create a writable stream
file.pipe(fstream); //pipe the post data to the file
//stream Ended - (data written) send the post response
req.on('end', function () {
res.writeHead(200, {"content-type":"text/html"}); //http response header
//res.end(JSON.stringify(arr)); //http response body - send json data
});
//Finished writing to stream
fstream.on('finish', function () {
console.log('Finished writing!');
//Get file stats (including size) for file saved to server
fs.stat(__dirname + '/img/' + filename, function(err, stats) {
if(err)
throw err;
//if a file
if (stats.isFile()) {
//console.log("It\'s a file & stats.size= " + JSON.stringify(stats));
console.log("File size saved to server: " + stats.size);
console.log("-----------------------");
};
});
});
// error
fstream.on('error', function (err) {
console.log(err);
});
}); // @END/ .req.busboy
}) // @END/ POST
//PUT
.put(function (req, res, next) {
var fstream;
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
console.log("Uploading: " + filename);
//Path where image will be uploaded
fstream = fs.createWriteStream(__dirname + '/img/' + filename);
file.pipe(fstream);
fstream.on('close', function () {
console.log("Upload Finished of " + filename);
res.redirect('back'); //where to go next
});
});
});
/* ==========================================================
Bind to a port and listen for connections on it
============================================================ */
var server = app.listen(3040, function() {
console.log('Listening on port %d', server.address().port);
console.log("========LISTENING==3040=======")
});