임에딘

add nodejs_fileupload.js

1 +var http=require('http');
2 +var formidable=require('formidable');
3 +var fs=require('fs');
4 +
5 +http.createServer(function(req,res){
6 + if (req.url=='/fileupload'){
7 + var form=new formidable.IncomingForm();
8 + form.parse(req,function(err,fields,files){
9 + var oldpath=fiels.filetoupload.path;
10 + var new path='C:/Users/njs04/'+file.filetoupload.name;
11 + fs.rename(oldpath,newpath,function(err){
12 + if (err) throw err;
13 + res.write('File uploaded');
14 + res.end();
15 + });
16 + });
17 + } else {
18 + res.writeHead(200,{'Content-Type': 'text/html'});
19 + res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
20 + res.write('<input type="file" name="firetoupload"><br>');
21 + res.write('<input type="submit">');
22 + res.write('</form>');
23 + return res.end();
24 + }
25 +}).listen(8080);