0
我正在創建Web服務以上傳一些文字圖像&文件類型應該只有圖像類型和強制性。我使用Muller從郵遞員上傳文件。我是選擇體型表單數據然後獲取表單數據。是否可以在控制器中編寫文件移動代碼?如何驗證文件類型圖像時發送郵件在nodejs?
路由碼是
var multer = require('multer');
var uploadPic = multer({dest:'public/uploads/profile'});
router.post('/v1/insertUser',uploadPic.any(),userController.saveUser);
控制器代碼是,我使用sequelize來插入數據。
userController.saveUser = function(req,res){
var params = req.body;
req.checkBody('first_name', 'The first name is required!').notEmpty();
req.checkBody('email', 'The email is required!').notEmpty();
req.checkBody('email', 'The email format is incorrect!').isEmail();
req.checkBody('boltt_code', 'The boltt code is required!').notEmpty();
var errors = req.validationErrors();
var userError = Array();
if(errors){
if(errors.length){
for(var key in errors){
userError.push({
'key' : errors[key].param,
'message' : errors[key].msg
});
}
}
return res.send({message: userError,status:'Failure',response:200,data:[]});
}
user.findOrCreate({
where:{
first_name: helper.trimInputValue(params.first_name),
email: helper.trimInputValue(params.email),
boltt_code: helper.trimInputValue(params.boltt_code)
},
defaults:{
first_name: helper.trimInputValue(params.first_name),
middle_name : '',
last_name : '',
email: helper.trimInputValue(params.email),
boltt_code: helper.trimInputValue(params.boltt_code),
image : req.files.filename,
}
})
.spread(function(user, created) {
var msg = '';
if(created){
msg = 'User inserted successfully!';
}else{
msg = 'User already exist!';
}
res.send({
message : msg,
status:'Success',
response:200,
data : user
});
}).catch(Sequelize.ValidationError,function(err){
data = err.errors;
var userError = Array();
if(data.length){
for(var key in data){
userError.push({
'key' : data[key].path,
'message' : data[key].message
});
}
}
res.send({
message : userError,
status : 'Failure',
response:200,
data:[]
});
}).catch((err) => {
res.send({
message : err,
status:'Failure',
response : 500,
data:[]
});
});
};