-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploadForm.js
More file actions
43 lines (41 loc) · 1.46 KB
/
uploadForm.js
File metadata and controls
43 lines (41 loc) · 1.46 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
var http = require('http');
var formidable = require('formidable');
var fs = require('fs');
var requestListener = function (req, res) {
'use strict';
var targetpath = '/home/alexis/Documents/';
if (req.url === '/fileupload') {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
var oldpath = files.filetoupload.path;
var newpath = targetpath + files.filetoupload.name;
fs.rename(oldpath, newpath, function (err) {
if (err) {
throw err;
}
res.write('File uploaded and moved!');
res.end();
});
if (err) {
res.write('<pre>Error: ' + err + '</pre>');
}
if (files) {
res.write('<pre>Files: ' + JSON.stringify(files) + '</pre>');
}
if (fields) {
res.write('<pre>Fields : ' + fields + '</pre>');
}
});
} else {
res.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8'
});
res.write('<pre>' + req.url + '</pre>');
res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
res.write('<input type="file" name="filetoupload"><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}
};
http.createServer(requestListener).listen(8080);