2012-09-18 60 views
0

我正在使用最新的NodeJS和ExpressJS编写一个小应用程序。目前我坚持上传文件。 :>使用ExpressJS上传文件

路由这种方式配置

app.get('/Share', share.index); 
app.post('/Share/Process', share.processFile); 

索引视图看起来如下

form.form-horizontal(action='/Share/Process', method='post') 
    legend share a file 
    div.control-group 
    label.control-label(for='item[actual]') File 
    div.controls 
     input(type='file', name='item[actual]', required='required') 

当我按照ExpressJS API文档,我应该对文件的访问,通过使用req.files,在share.processFile方法中未定义

exports.processFile = function(req,res){ 
    console.log(req.files); // undefined 
    console.log(req.body.item.actual); // filename as string 
    res.render('share/success', {navigation: { shareClass : 'active'}, downloadKey: 'foo'}); 
}; 
+0

你确定在定义路由之前调用'express.bodyParser'(否则'req.files'不会被设置)? – ebohlman

+0

是bodyParser被调用而没有任何参数 –

回答

2

尝试更改表单编码为multipart/form-data,IIRC应该设置为传输文件。

+0

的作品!感谢:D –