Thursday, 11 May 2017

How do I insert video in folder using nodejs and mongodb?

// Video Post

router.post('/videopost',function(req,res){
    console.log(req.body);

    var postvideoname= Uuid.create();

    var post = new Post();
    post.post_title=req.body.videotitle;
    post.post_video=postvideoname+'.mp4';
    post.user_id=req.body.user_id;
    post.first_name=req.body.first_name;
    post.last_name=req.body.last_name;
    post.profile_pic=req.body.profile_pic;
    post.inserted=Date.now();
    post.isvideopost=1;

    post.save(function(err,data){
        if(err){
            console.log(err);
        }
        else{
            var dir='public/uploads/leadervideo/';

            if(!fs.existsSync(dir))
                    fs.mkdirsSync(dir);

            if(req.body.postvideo){
            require("fs").writeFile(dir+postvideoname+'.mp4', req.body.postvideo, 'base64', function(err) {
                    if(err) console.log(err);
                })
            }

res.json({success:true,data:data,dir:'uploads/leadervideo/'});
        }
    });
})

This function inserting the data in mongodb very well, even it is inserting video name with extension but i am unable to see video in folder and at the time of fetching it is showing corrupted video.... please give me solution???



via G. Sagar