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
Post a Comment