Dar Hamid
Dar Hamid

Reputation: 1979

video upload using node.js and mongodb

I am New to MongoDB and node.js. I want to know how can I store and fetch a video file in MongoDB using node.js? Any help will be appreciated. The code that I am using is:

app.post('/videos/new', function(req, res) {
  req.form.complete(function(err, fields, files) {
    console.log('here i go');
    if(err) {
      next(err);
    } else {
      ins = fs.createReadStream(files.file.path);
      console.log('insssssssssssss'+ins);
      ous = fs.createWriteStream(__dirname + '/static/uploads/videos/' + files.file.filename);
      util.pump(ins, ous, function(err) {
        if(err) {
          next(err);
        } else { RegProvider.save({
           file: req.param(files.file.filename),
                   filename: req.param('filename')
                 }, function(error, docs) {
              res.redirect('/videos');
         });
        }
      });
      //console.log('\nUploaded %s to %s', files.file.filename, files.file.path);
      //res.send('Uploaded ' + files.file.filename + ' to ' + files.file.path);
    }
  });
});

The code to retrieve video files is:

var fs = require('fs');

var src_path = __dirname + '/static/uploads/vidoes/'
module.exports.list = function(callback) {
  fs.readdir(src_path,function(err, files){
    var ret_files = [];
    files.forEach(function(file) {
    ret_files.push('/uploads/videos/' + file);
    });
    console.log(ret_files);
    callback(err, ret_files);
  });
};

It is working fine, but doesn't store anything in MongoDB.

Upvotes: 0

Views: 4918

Answers (1)

Chris Biscardi
Chris Biscardi

Reputation: 3153

https://github.com/marcello3d/node-mongolian is useful for string files in GridFS.

There are more examples of how to use the library than I can post here in the examples folder.

here: https://github.com/marcello3d/node-mongolian/blob/master/examples/mongolian_trainer.js

If you are uncertain of what GridFS is you can find out here: http://www.mongodb.org/display/DOCS/GridFS

and thanks for improving your question.

Upvotes: 2

Related Questions