0

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?

4

1 に答える 1

2

Express3bodyParser()formidable内部で使用します。したがって、これは機能するはずです。

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;
    });
于 2012-12-25T02:52:34.587 に答える