Reputation: 15626
I want use formidable with express in node.js to achieve upload image function,
what I do is :
app.configure(function () {
app.use(express.static(__dirname + "/media"));
app.use(express.bodyParser());
})
app.post('/upload', function (req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files){
console.log("log in parse");
console.log("fields type is " + typeof fields);
console.log("files type is " + typeof files);
console.log(files.image);
if (err) return res.send('You found error');
});
})
with this code, the image could upload successully, but the form.parse
function seems doesn't been invoked, cuz the log
doesn't been invoked
Why?What's wrong with my code?
Upvotes: 0
Views: 1220
Reputation: 5007
express 3 bodyParser()
uses formidable
internally.
So this should work:
view.jade
form#fileupload(enctype="multipart/form-data")
input(type="hidden",name="user[id]", value="1")
input(type="file",name="photo[file]")
app.js
app.post('/upload', function (req, res) {
var userId = req.body.user.id;
var photo = req.files.photo.file;
});
Upvotes: 2