nodejs_fileupload.js 823 Bytes
var http=require('http');
var formidable=require('formidable');
var fs=require('fs');

http.createServer(function(req,res){
  if (req.url=='/fileupload'){
    var form=new formidable.IncomingForm();
    form.parse(req,function(err,fields,files){
      var oldpath=fiels.filetoupload.path;
      var new path='C:/Users/njs04/'+file.filetoupload.name;
      fs.rename(oldpath,newpath,function(err){
        if (err) throw err;
        res.write('File uploaded');
        res.end();
      });
    });
  } else {
    res.writeHead(200,{'Content-Type': 'text/html'});
    res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
    res.write('<input type="file" name="firetoupload"><br>');
    res.write('<input type="submit">');
    res.write('</form>');
    return res.end();
  }
}).listen(8080);