node.js Express 无法上传文件,req.files 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21128451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Express can't upload file, req.files is undefined
提问by hulufei
I really apologize if I'm leaving something out and am totally stupid, but I've checked and checked over again a number of times, and the file upload functionality is just not working over here. I made a super minimal app to demonstate. Just generated a new express app with the most up-to-date version (3.4.7) and added the least i could to make a file upload work.
如果我遗漏了一些东西并且完全愚蠢,我真的很抱歉,但是我已经检查并再次检查了很多次,并且文件上传功能在这里不起作用。我制作了一个超级最小的应用程序来演示。刚刚使用最新版本 (3.4.7) 生成了一个新的 express 应用程序,并添加了最少的内容来使文件上传工作。
Here's my app.js file
这是我的 app.js 文件
/**
* Module dependencies.
*/
var express = require('express');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/tasks', function(req, res) {
res.render('form');
});
app.post('/tasks', function(req, res) {
console.log(req.files);
res.send('ok');
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
exports = module.exports = app;
And here's my form.jade view file:
这是我的 form.jade 视图文件:
doctype html
html
head
title Task Upload
body
form(action='/tasks', method='post', enctype='multipart/form-data')
input(name='task', type='file')
input(type='submit')
Everytime I try to upload a file, req.fileslogs out undefined. Can anyone save me out from this problem?
每次我尝试上传文件时,都会req.files注销undefined。谁能把我从这个问题中解救出来?
采纳答案by dinukadev
Add the following in your app.js
在 app.js 中添加以下内容
app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyParser({keepExtensions:true,uploadDir:path.join(__dirname,'/files'}));
});
And then try to access as follows;
然后尝试如下访问;
req.files.task
It is recommended not to use bodyParser, but to simply define the type of handling you want. In your case since its file uploading, you can enable it as follows
建议不要使用 bodyParser,而是简单地定义您想要的处理类型。在您的情况下,因为它的文件上传,您可以按如下方式启用它
app.configure(function(){
app.use(express.methodOverride());
app.use(express.multipart());
});
You can read about why using bodyParser() is not a good idea in the following link.
您可以在以下链接中了解为什么使用 bodyParser() 不是一个好主意。
http://andrewkelley.me/post/do-not-use-bodyparser-with-express-js.html
http://andrewkelley.me/post/do-not-use-bodyparser-with-express-js.html
回答by Red Efire
In Express 4, req.filesis no longer available on the reqobject by default. To access uploaded files on the req.filesobject, use multipart-handling middleware like busboy, multer, formidable, multiparty, connect-multiparty,.
在Express 4 中,默认情况下req.files在req对象上不再可用。要访问req.files对象上上传的文件,请使用多部分处理中间件,如busboy、multer、formidable、multiparty、connect-multiparty 等。

