我正在使用dropzone从UI上传文件。这是我的drozone配置:
passport.callback = function (req, res, next) {
var provider = req.param('provider') || 'local'
, action = req.param('action');
// Passport.js wasn't really built for local user registration, but it's nice
// having it tied into everything else.
if (provider === 'local' && action !== undefined) {
if (action === 'register' && !req.user) {
this.protocols.local.register(req, res, next);
}
else if (action === 'connect' && req.user) {
this.protocols.local.connect(req, res, next);
}
else if (action === 'disconnect' && req.user) {
this.protocols.local.disconnect(req, res, next);
}
else {
next(new Error('Invalid action'));
}
} else {
if (action === 'disconnect' && req.user) {
this.disconnect(req, res, next) ;
} else {
// The provider will redirect the user to this URL after approval. Finish
// the authentication process by attempting to obtain an access token. If
// access was granted, the user will be logged in. Otherwise, authentication
// has failed.
this.authenticate(provider, next)(req, res, req.next);
}
}
};
我正在使用node和expressjs来构建基于this link
的服务器端端点我在服务器端的代码下面打扰。
var imageUpload = new Dropzone('div#dataSection', {
url: 'api/image',
autoProcessQueue: true,
paramName: 'file'
});
问题是req.files以未定义的方式出现。我错过了什么吗?
答案 0 :(得分:3)
您关注的tutorial已经过时了,而且肯定是为express
的旧版本编写的。 2014年4月发布的express
4.0(差不多)all built-in middleware was dropped,其中包括一个解析上传文件req.files
。
这就是问题,解决方案是使用单独的模块来处理上传的文件。 Multer
似乎是一个自然的选择(对于这个建议,感谢@Ben Fortune。)
首先,安装multer
Node.js模块:
npm install multer
然后,在服务器端的代码中:
var express = require('express')
var multer = require('multer')
var app = express()
app.use(multer({ dest: './uploads/'})) // directory to store uploaded files in
之后,所有POST
个ed文件应在路由处理函数中以req.files
的形式提供,格式为Multer file objects。