hh54188
hh54188

Reputation: 15626

Upload image with formidable in node.js failed

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

Answers (1)

Cris-O
Cris-O

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

Related Questions