Store files in mongodb using mongoose

I am new to node and mongoDB. I am developing an app with nodeJS, express and mongoDB. I want to read a csv / xlsx file from a file input field and save it to mongoDB using mongoose. I'm having difficulties. I am using angularjs in my frontend. Can anyone give me suggestions on what procedure should I go through? The specific code will be of great help.

I used the 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 interface:

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

      

There are still no errors. Now I just want to store these files in a database. What should I do next?

+3


source to share


1 answer


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

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

I used below to save the file to 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 downloading the file, and this is not a finished proof of concept. Note that I've replaced busboy with formidable as a personal preference.

Does it help you move on?

SC1

+4


source







All Articles