ishakya
ishakya

Reputation: 289

store files in mongodb using mongoose

I am new with node and mongoDB. I am developing an app with nodeJS, express and mongoDB. I want read a csv/xlsx file from a file input field and store it in mongoDB using mongoose. I am having difficulties. I am using angularjs in my front end. Can anyone give me suggestions about what procedure I should go through? Specific Code will be great help.

I used busboy module to store files in a specific folder. Here is my code

In routes:

router.post('/fileupload', function (req, res) {
    var fstream;
    req.pipe(req.busboy);
    console.log(req.pipe);
    console.log(req.busboy);
    req.busboy.on('file', function (fieldname, file, filename) {
        console.log("Uploading: " + filename);
        fstream = fs.createWriteStream('./files/' + filename);
        file.pipe(fstream);
        fstream.on('close', function () {
            res.redirect('back');
        });
    });
});

and my frontend:

<form method="post" action="/fileupload" enctype="multipart/form-data">
    <input type="file" id="file" name="file">
    <button type="submit">Submit</button>
</form>

Up to now there is no error. Now i just want to store these files in database. What should I do next?

Upvotes: 8

Views: 20885

Answers (1)

sundayoyeniyi
sundayoyeniyi

Reputation: 595

You probably will need to understand the basis for storing files in mongo. Why are files stored in mongo and how they are stored?

From what you have done, you now need a mongoose plugin that will store your uploaded file into mongo GridFS. The GridFS is here:- http://docs.mongodb.org/manual/core/gridfs/. You can use any compatible driver to access the grid - mongoose and mongoose plugin gridfs-stream are an example - see here: https://www.npmjs.org/package/gridfs-stream

I have used below to save a file into the gridfs.

var express = require('express');
var formidable = require('formidable');
var mongoose = require('mongoose');
var grid = require('gridfs-stream');
var fs = require('fs');
var util = require('util');
var app = express();

app.post('/fileupload', function (req, res) {
    var form = new formidable.IncomingForm();
    form.uploadDir = __dirname + "/data";
    form.keepExtensions = true;
    form.parse(req, function(err, fields, files) {
        if (!err) {
          console.log('File uploaded : ' + files.file.path);
          grid.mongo = mongoose.mongo;
          var conn = mongoose.createConnection('..mongo connection string..');
          conn.once('open', function () {
          var gfs = grid(conn.db);
          var writestream = gfs.createWriteStream({
              filename: files.file.name
          });
          fs.createReadStream(files.file.path).pipe(writestream);
       });
     }        
   });
   form.on('end', function() {        
       res.send('Completed ..... go and check fs.files & fs.chunks in  mongodb');
   });

});

app.get('/', function(request, response){
    response.send(
        '<form method="post" action="/fileupload" enctype="multipart/form-data">'
        + '<input type="file" id="file" name="file">'
        + '<input type="submit" value="submit">'
        + '</form>'
        );    
});

app.listen(40000, function() {
    console.log('Express is listening on port 40000');
});

The above is a guide on how you should proceed after uploading your file and it is not a production ready proof of concept. Note that I replaced busboy with formidable as a personal preference.

Does it help you to move on?

SO1

Upvotes: 9

Related Questions