node.js - how to upload a file in nodejs -


html code:-

<html> <body> <form action="http://localhost:3000/api" method="post" enctype="multipart/form-data">     <input type="file" name="file">     <input type="submit"> </form> </body> </html> 

the code in nodejs is

app.post('/api',function (req, res) {     //json.stringify(req.files);     console.log(json.stringify(req.files));     var file = req.files.file;     var stream = fs.createreadstream(file.path); }); 

i want print details of the-files.

i getting error

typeerror: cannot read property 'file' of undefined @ /users/maddy/desktop/check1/server.js:27:25

use multer, express/connect no longer supports multipart on own. also, use post method on express/connect app.

after installing multer:

var multer  = require('multer'); var upload = multer({dest:'uploads/'});  app.post('/api', upload.single('file'), function (req, res) {    res.sendstatus(200);  }); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -