./路由/ index.js
var keystone = require('keystone');
middleware = require('./middleware');
importRoutes = keystone.importer(__dirname);
User = keystone.list('User');
multer = require('multer');
keystone.pre('routes', middleware.initLocals);
keystone.pre('render', middleware.flashMessages);
var routes = {
views: importRoutes('./views'),
};
exports = module.exports = function (app) {
//multer uplaoding to local destination
const storage = multer.diskStorage({
destination: function (req, file, cb) {
console.log(req);
cb(null, '../public/images')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
const upload = multer({
storage: storage
}).single('profilePic');
// Views
app.get('/', middleware.requireUser, routes.views.index);
app.get('/blog/:category?', routes.views.blog);
app.get('/blog/post/:post', routes.views.post);
app.get('/gallery', routes.views.gallery);
app.get('/research', routes.views.research);
app.get('/publications', routes.views.publications);
app.get('/profile', function (req, res) {
res.render('profile.hbs');
});
app.get('/teaching-initiatives', routes.views.teaching);
app.post('/upload', (req, res) => {
var updatedFirst = req.body.first_name;
var updatedLast = req.body.last_name;
if (updatedFirst != '' && updatedLast != '') {
req.user.name.first = updatedFirst;
req.user.name.last = updatedLast;
}
// console.log(req.user.name.first);
req.user.save(function (saveError) {
if (saveError) {
console.log(saveError);
}
});
upload(req, res, (err) => {
if (err) {
res.render('profile', {
msg : err
});
} else {
console.log(req);
res.redirect('/profile');
//res.send('test');
}
})
});
app.all('/contact', routes.views.contact);
};
./模板/视图/ public.hbs
<div class="container-fluid profile">
<div class="row">
<form action="/upload" method="post" class="col s12 m12 l12 z-depth-2" enctype="multipart/form-data">
<div class="row">
<div class="input-field col s12">
<input id="first_name" name="first_name" type="text" class="validate" data-length="15">
<label for="first_name">First Name</label>
</div>
<div class="input-field col s12">
<input id="last_name" name="last_name" type="text" class="validate" data-length="15">
<label for="last_name" data-error="wrong" data-success="right">Last Name</label>
</div>
</div>
<button type="submit" class="btn">Update<i class="material-icons right">send</i></button>
<div class="file-field input-field">
<div class="btn grey">
<span>Profile Pic</span>
<input name="profilePic" type="file" >
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
<button type="submit" class="btn waves-effect waves-light btn">Upload <i class="material-icons right">file_upload</i></button>
</div>
</form>
</div>
我在另一个具有基本快速结构的项目中对此进行了测试。它工作得很好。但是在这里它正在提到目的地,而不是去本地系统上的临时文件夹。
我已查看enctype='multipart/form-data'
&amp; fieldname
也是。但我不知道出了什么问题。这是请求吗?