Ravi Shankar Bharti
Ravi Shankar Bharti

Reputation: 9268

How to save PDF as BLOB in MongoDB Node app

I am creating a node App. I need to save PDF in mongoDB, i am facing issues achieving this.

Currently i am using ng-file-upload to upload the PDF to server and multer on node to store it on the server. Then, i save the location of the PDF in Mongo Document.

But, what i want is to store the file directly as a BLOB in the document, not 'file location in server'. Is there a way to achieve this?

Controller

$scope.upload = function (file)
{
    Upload.upload({
        url: 'uploadImage',
        data: {file: file}
    }).then(function (resp) {
        console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
    }, function (resp) {
        console.log('Error status: ' + resp.status);
    }, function (evt) {
        var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
        console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
    });
};

Node - Multer

router.all('/uploadMulter',function (req,res,next) {
   upload(req,res,function(err) {
       if(err) {
           console.log('Error uploading file');
           return res.end("Error uploading file.");
       }
       console.log('success');
       res.end("File is uploaded");
   });
});

Upvotes: 0

Views: 1299

Answers (1)

Prasad Sonawane
Prasad Sonawane

Reputation: 72

Read file using blob and take data in binary format and push in db.

Upvotes: 1

Related Questions