使用Express上传文件失败:“无法读取未定义的属性文件”

时间:2014-02-19 10:03:57

标签: node.js express pug

我想上传一个像这样的表格(JADE)的文件:

form(action="/file-upload", name="upload", method="post", enctype="multipart/form-data")
    input(type="file", name="theFile")
    input(type="submit", name="Upload")

这是我的app.js:

app.use(express.methodOverride());
app.use(express.multipart());
app.use(express.bodyParser());

这就是我处理请求的方式:

app.post('/file-upload',function(req,res){
   console.log('FIRST TEST: ' + JSON.stringify(req.files));
   console.log('second TEST: ' +req.files.theFile.name);
});

这是我的Node.js控制台:

FIRST TEST: undefined
TypeError: Cannot Read Property 'theFile' of undefined....etc....

哦,顺便提一下Connect模块发出警告:

connect.limit() will be removed in connect 3.0
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives

我在这里做错了什么?我已按照hereherehere提供的建议,但没有任何作用。谢谢你的帮助......

更新: 我刚刚将LOGGER添加到我的应用程序结果中,所以看起来我收到了请求:

127.0.0.1 - - [Wed, 19 Feb 2014 11:02:11 GMT] "POST /file-upload HTTP/1.1" 500 - 
"http://localhost:3000/admin/news-add" "Mozilla/5.0 (Windows NT 6.1; WOW64) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36"

1 个答案:

答案 0 :(得分:3)

我现在看到......我有

app.use(express.multipart());
app.use(express.bodyParser());
app.use(express.methodOverride());

app.use(app.router);之后应该在之前声明,如下所示:

app.use(express.multipart());
app.use(express.bodyParser());
app.use(express.methodOverride())
app.use(app.router);

现在,它的工作就像一个魅力......