node.js upload file 上传文件


var http = require('http')
,formidable = require('formidable')
,fs = require('fs')
, sys = require('sys');

http.createServer(function (req, res) {
// set up some routes
switch(req.url) {
case '/':
// show the user a simple form
console.log("[200] " + req.method + " to " + req.url);
res.writeHead(200, "OK", {'Content-Type': 'text/html'});
res.write('<html><head><title>Hello Noder!</title></head><body>');
res.write('<h1>Welcome Noder, who are you?</h1>');
res.write('<form enctype="multipart/form-data" action="/formhandler" method="post">');
res.write('Name: <input type="text" name="username" value="John Doe" /><br />');
res.write('Age: <input type="text" name="userage" value="99" /><br />');
res.write('File :<input type="file" name="upload" multiple="multiple"><br>');
res.write('<input type="submit" />');
res.write('</form></body></html');
res.end();
break;
case '/formhandler':
if (req.method == 'POST') {
console.log("[200] " + req.method + " to " + req.url);

req.on('data', function(chunk) {
console.log("Received body data:");
// console.log(chunk.toString());
});
var form = new formidable.IncomingForm();
form.parse(req, function(err,fields, files) {
console.log('in if condition'+sys.inspect({fields: fields, files: files}));
fs.writeFile(files.upload.name, files.upload,'utf8', function (err) {
if (err) throw err;
console.log('It\'s saved!');
});

res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end();
});
req.on('end', function() {
// empty 200 OK response for now
res.writeHead(200, "OK", {'Content-Type': 'text/html'});
res.end();
});

} else {
console.log("[405] " + req.method + " to " + req.url);
res.writeHead(405, "Method not supported", {'Content-Type': 'text/html'});
res.end('<html><head><title>405 - Method not supported</title></head><body><h1>Method not supported.</h1></body></html>');
}
break;
default:
res.writeHead(404, "Not found", {'Content-Type': 'text/html'});
res.end('<html><head><title>404 - Not found</title></head><body><h1>Not found.</h1></body></html>');
console.log("[404] " + req.method + " to " + req.url);
};
}).listen(8081)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值