javascript - How to upload image in Node.js -
i complete newbie node.js.i want learn upload , showing images using ajax in php.i found of tutorials tough me.to started have tried using code
var express = require("express"); var app = express() var bodyparser = require('body-parser') //all environment app.use(bodyparser()) var form = "<!doctype html><html><body>" + "<form method='post' action='/upload' enctype='multipart/form-data'>" + "<input type='file' name='image'/>" + "<input type='submit' /></form>" + "</body></html>"; app.get('/', function (req, res){ res.writehead(200, {'content-type': 'text/html' }); res.end(form); }); /// post files app.post('/upload', function(req, res) { console.log('hello world'); console.log(req.files); }); app.listen(8080)
but getting undefined req.files.can tell why?.please forgive me if stupid question.and resources.thank in advance.
req.files express v3, using v4.
now body-parser handles urlencoded , json bodies. multipart bodies should use alternative.
https://github.com/expressjs/body-parser
for example multer:
var express = require('express') var multer = require('multer') var app = express() app.use(multer({ dest: './uploads/'})) /// post files app.post('/upload', function(req, res) { console.log('hello world'); console.log(req.files); }); app.listen(8080)
Comments
Post a Comment