我正在尝试使用multer上传文件。文件未上传。 Pl让我知道代码的问题是什么。 根据multer文档,req.body携带文件,但未定义。
var express = require('express');
var multer = require('multer');
var jade = require('jade');
var upload = multer({dest: 'uploads/'});
var app = express();
app.set('view engine', 'jade');
app.set('views', './views');
app.get('/signup', function (req, res){
res.render('signup');
});
app.post('/signup', upload.single('profile_images'), function(req, res){
if(req.file){
console.log(util.inspect(req.file));
}
res.json(req.body);
});
app.listen(3000, function() {
console.log('Server started');
});
html
head
title #{title}
link(rel='stylesheet', href='/stylesheets/style.css')
body
h1 #{title}
p Enter your name and email address to become a member.
form(action='/signup', method='post', enctype='multipart/formdata')
div
label Name
input(type='text', name='name')
div
label Email
input(type='text', name='email')
div
label Profile Image
input(type='file', name='profile_image')
div
input(type='submit')
答案 0 :(得分:0)
问题与表单属性enctype有关。添加破折号(multiparty / form-data)后,其工作正常。