Tuesday, 2 May 2017

Change filename with multer NodeJs

I have to upload a file which the name depends of a varible sended to the request. So I have a variable declared, imageType, to use in the multer.diskStoragean I have another variable for de dynamic path too, imagePath which it's working in the same way that I'm using the imageType, but I'm getting undefined-logo.jpg from the imageType. Here is the code:

var imagePath = '';
var imageType = '';
var storage = multer.diskStorage({
    destination: function (req, file, callback) {
        callback(null, imagePath);
    },
    filename: function (req, file, callback) {
        callback(null, (imageType + '-logo.jpg'));
    }
});

var upload = multer({ storage: storage }).single('file');

router.route('/representatives/:id/loginLogo')
    .post((req, res, next) => {
    /////SOME CODE BEFORE/////
    if (rows[0].client_directory == '') {
        imagePath = `${rows[0].subdomain_directory}`;
    } else {
        imagePath = `${rows[0].subdomain_directory}/${rows[0].client_directory}/`;
    }
    imageType = req.body.imageType;
    /////SOME CODE AFTER///// 
})

Could you help me? Thanks!



via Crisiiii

No comments:

Post a Comment