Expressjs bodyParser和connect-form [英] Expressjs bodyParser and connect-form
问题描述
我使用连接表单上传图片。但是如果我使用 bodyParser()
,则不起作用。
另一种方式,如果我不使用bodyParser,我不能上传文件?
Im uploading images with connect form. But it does not work if I use the bodyParser()
.
And the other way around, if i don't use the bodyParser, i cant upload files?
如何让他们一起玩?这是我的配置:
How do I make them play together? Here is my config:
app.configure(function() {
app.register('.html', require('ejs'));
app.set('views', __dirname + '/../views');
app.set('view engine', 'html');
app.use(gzippo.staticGzip(__dirname + '/../public'),{ maxAge: 86400000 });
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(form({
keepExtensions: true,
uploadDir: __dirname + '/../tmp'
}));
app.use(express.cookieParser());
app.use(express.session({
secret: 'test',
cookie: { secure: true },
store: new MySQLSessionStore(client.database, client.user, client.password)
}));
app.use(expressValidator);
app.use(app.router);
app.use(express.csrf());
});
推荐答案
如果您使用最新的Express,需要包含connect-form(自1.8.x以来已被弃用)。
If you are using the latest Express you don't need to include connect-form (which is deprecated since Connect 1.8.x).
只需在路由中使用req.files即可获取上传的文件,Express会执行其他操作。查看这篇文章:
Just use req.files in your routes to get the uploaded files, Express does the rest. Check out this post:
http://tjholowaychuk.com/post/12943975936/connect-1-8-0-multipart-support
这篇关于Expressjs bodyParser和connect-form的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!