express multer无法读取未定义的属性"profileimage" [英] express multer Cannot read property 'profileimage' of undefined
问题描述
我正在尝试提交一个表单,其中包含一个名称为profileimage
的文件输入,我正在使用express 4,express generator和multer.
I am trying to submit a form which has a file input within it with a name of profileimage
, i am using express 4, express generator and multer.
在我的app.js
中,我像这样添加了multer:
In my app.js
I added multer like so:
var multer = require('multer');
var upload = multer({ dest: './uploads' });
我要遵循的教程实际上设置了multer,就像这样(在需要它之后),但是它给了我一个错误:
The tutorial i am following actually sets up multer like so (after requiring it) but it gives me an error:
app.use(multer({dest: './uploads'}));
在相应文件内的routes
文件夹中,我具有以下内容:
And in my routes
folder inside the respective file I have the following:
router.post('/register', function(req, res, next) {
// get form values
var name = req.body.name;
var email = req.body.email;
var username = req.body.username;
var password = req.body.password;
var password2 = req.body.password2;
// check for image field
if (req.files.profileimage)
{
console.log('Uploading file...');
...
但是,当我提交表单时,出现以下错误:
However when I submit the form I get the following error:
Cannot read property 'profileimage' of undefined
似乎无法理解req.files.profileimage
,但是我不知道为什么?
It seems to not be able to understand req.files.profileimage
, but I don't know why?
推荐答案
multer
的API不久前发生了变化,因此那里的一些教程仍然使用旧的API.
The API for multer
changed a bit some time ago, so some tutorials out there still use the old API.
对于单个文件,您将执行以下操作:
For a single file, you would do something like:
var uploads = multer({dest: './uploads'});
// ...
router.post('/register', uploads.single('profileimg'), function(req, res, next) {
// ...
if (req.file) {
console.log('Profile image uploaded');
}
});
有关其他情况的更多示例,请参见 multer
文档
See the multer
documentation for more examples of other scenarios.
这篇关于express multer无法读取未定义的属性"profileimage"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!