form.parse(req, function (err, fields, files) { var dir = __dirname + '/upload'; if (!path.existsSync(dir)) { fs.mkdirSync(dir, 0744); } var oldpath = files.filetoupload.path; var newpath = dir + files.filetoupload.name; fs.rename(oldpath, newpath, function (err) { if (err) throw err; res.write('File uploaded and moved!'); res.end(); }); });
http.createServer(function(req,res){ if(req.url=='/fileupload'){ var form=new formidable.IncomingForm(); form.parse(req,function(err,fields,files){ var oldpath=files.filetoupload.path; var newpath='C:/Users/utsav/Documents/Github/nodejsExamples/'+files.filetoupload.name; fs.rename(oldpath,newpath,function(err){ if(err) throw err; res.write('File uploaded and moved!'); 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="fileupload"><br>'); res.write('<input type="submit">'); res.write('</form>'); return res.end(); } }).listen(8080);
var form = new formidable.IncomingForm(); form.parse(req, function (err, fields, files) { var oldpath = files.filetoupload.path; var newpath = './upload/upload' + files.filetoupload.name; fs.rename(oldpath, newpath, function (err) { if (err) throw err;