Repository navigation
Expand file tree
/
Copy pathrequestHandle.js
More file actions
61 lines (53 loc) · 1.58 KB
/
Copy pathrequestHandle.js
File metadata and controls
61 lines (53 loc) · 1.58 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
var querystring = require("querystring"),
fs = require("fs"),
formidable = require("formidable");
function start(res) {
console.log("req handler 'start' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html; '+
'charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="file" name="upload" multiple="multiple">'+
'<input type="submit" value="Upload file" />'+
'</form>'+
'</body>'+
'</html>';
res.writeHead(200, {"Content-Type": "text/html"});
res.write(body);
res.end();
}
function upload(res, req) {
console.log("Request handler 'upload' was called.");
var form = new formidable.IncomingForm();
form.uploadDir = "tmp";
console.log("about to parse");
form.parse(req, function(error, fields, files) {
console.log("parsing done");
fs.renameSync(files.upload.path, "./tmp/test.png");
res.writeHead(200, {"Content-Type": "text/html"});
res.write("received image:<br/>");
res.write("<img src='/show' />");
res.end();
});
}
function show(res) {
console.log("Request handler 'show' was called.");
fs.readFile("./tmp/test.png", "binary", function(error, file) {
if(error) {
res.writeHead(500, {"Content-Type": "text/plain"});
res.write(error + "\n");
res.end();
} else {
res.writeHead(200, {"Content-Type": "image/png"});
res.write(file, "binary");
res.end();
}
});
}
exports.start = start;
exports.upload = upload;
exports.show = show;