如何在nodejs中从邮递员发送文件时验证文件类型图像?

时间:2017-08-29 10:14:59

标签: node.js multer

我正在创建网络服务来上传带有文字和图片的图片。文件类型应该只是图像类型和强制。我正在使用multer从邮递员上传文件。我选择body type form-data然后获取表单数据。是否可以在控制器中写入文件移动代码。

路线代码

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:[]
        });
    });
};

1 个答案:

答案 0 :(得分:0)

var multer = require('multer');

var storage = multer.diskStorage({
    destination : function(req,file,cb){
        cb(null,'public/uploads/profile/');
    },
    filename : function(req,file,cb){
        if(!file.originalname.match(/\.(png)$/)){
            var err = new Error();
            err.code = 'filetype';
            return cb(err);
        }else{
            cb(null,file.originalname + '_' + Date.now());
        }
    }
});

var upload = multer({
    storage : storage,
    limits : {fileSize : 10000000}
}).single('image');

此代码工作正常。