Monday, 8 May 2017

How to upload PDF files using nodejs

Is it possible to upload a PDF file using multer or if there is any other solution for it would be helpful? If I am trying to change the name of the file while storing in the folder then the file is not storing .I want to store the file with its extension.

router.post('/speaker', upload.single('profile_pic'), function (req, res) {
    var speakerForm = JSON.parse(req.body.speaker);
    var mail=speakerForm.email;
    var uID = speakerForm.uid;
    Speaker.find({email:mail,uid:uID},function(err,speaker){
        if(err){
            return res.send(500, err);
        }
        if(speaker.length>0){
            res.send({state: 'failure', message:"Speaker already exist." });
        }
        else{
            var speaker = new Speaker();
            speaker.name = speakerForm.name;
            speaker.job_title = speakerForm.job_title;
            speaker.email = speakerForm.email;
            speaker.company = speakerForm.company;
            speaker.description = speakerForm.description;
            speaker.uid = speakerForm.uid;
            speaker.event_id = speakerForm.event_id;
            speaker.facebookUrl = speakerForm.facebookUrl;
            speaker.twitter = speakerForm.twitter;
            speaker.linkedIn = speakerForm.linkedIn;
            speaker.gmail = speakerForm.gmail;
            if(req.file){
                console.log('req.file',req.file);
                console.log('req.file',req.file);
                var profile_pic = req.file;
                profile_pic.originalname = req.file.originalname;
                profile_pic.destination = "uploads/";
                console.log("path.parse(profile_pic.originalname).name",path.parse(profile_pic.originalname).name);
                profile_pic.path = "uploads/"+path.parse(profile_pic.originalname).name;
                profile_pic.fileExtension = path.parse(profile_pic.originalname).ext;
                speaker.profile_pic = profile_pic;
            }
             
            speaker.save(function(err,speakerobj){
                if(err){
                  return res.send(500, err);
                }
                return res.json(speakerobj);
            });
        }
    })
});


via Ikshit Mathur

No comments:

Post a Comment