jeudi 1 septembre 2016

using if statement with req.file for a single file upload

I have a basic web app where i want the users to be able to register with a picture upload in the form, but if they do not have a picture to upload at the current time i want to automatically set a default image that is already stored locally in uploads folder as noimage.png to be called and stored as the profile image of the user. This has been my trial but not working;

var multer = require('multer');


//var uploads = multer({dest: './uploads/'});

var storage = multer.diskStorage({
    destination: function (request, file, callback) {
        callback(null, './uploads');
    },
    filename: function (request, file, callback) {
        console.log(file);
        callback(null, file.fieldname)
    }
});
var upload = multer({ storage: storage });
router.post('/register', upload.single('profileimage'), function(req,res,next){

    `if(req.file.profileimage){
            console.log('uploading File...');

            // File Info
            var profileImageOriginalName = req.files.profileimage.originalname;
            var profileImageName = req.files.profileimage.name;

            var profileImageMime = req.files.profileimage.mimetype;
            var profileImagePath = req.files.profileimage.path;
            var profileImageExt = req.files.profileimage.extension;
            var profileImageSize = req.files.profileimage.size;
        } else {
            // Set a Default Image
            var profileImageName = 'noimage.png';
        }

How can i use multer to handle this?

Aucun commentaire:

Enregistrer un commentaire