Stream formidable uploads into MongoDB GridFS
Latest commit 2b9f17b Jun 23, 2014 @aheckmann release 0.1.7
Failed to load latest commit information.
lib gridfs-stream@0.5.0 createWriteStream Jun 23, 2014
test tests; default safe writes Dec 26, 2012
.gitignore initial release May 11, 2012 release 0.1.7 Jun 23, 2014
LICENSE initial release May 11, 2012
Makefile test; added manual test Sep 1, 2012 requires mongodb not mongo Jan 17, 2013
package.json release 0.1.7 Jun 23, 2014


Formidable streams uploads to the file system by default. If you're using GridFS to store files you'll then need to turn around and copy them off of the file system. Using gridform removes this burden.


var mongo = require('mongodb')
var gridform = require('gridform');

// assuming you've already created a db instance and opened it
gridform.db = db;
gridform.mongo = mongo;

// in your http server
var app = http.Server(function (req, res) {

  // create a gridform
  var form = gridform();

  // returns a custom IncomingForm
  assert(form instanceof formidable.IncomingForm);

  // optionally store per-file metadata
  form.on('fileBegin', function (name, file) {
    file.metadata = 'so meta'

  // parse normally
  form.parse(req, function (err, fields, files) {

    // use files and fields as you do today
    var file = files.upload; // the uploaded file name
    file.type // file type per [mime](
    file.size // uploaded file size (file length in GridFS) named "size" for compatibility
    file.path // same as included for compatibility
    file.lastModified // included for compatibility

    // files contain additional gridfs info
    file.root // the root of the files collection used in MongoDB ('fs' here means the full collection in mongo is named 'fs.files')   // the ObjectId for this file



npm install gridform


The module exports a function which takes an options object.

var gridform = require('gridform');
var options = { db: db, mongo: mongo, filename: fn };
var form = gridform(options);

Available options:

The optional filename function is passed the before streaming to MongoDB providing an opportunity to return a customized filename with a prefix etc.

db and mongo are required unless you've specified them on gridform itself.

var gridform = require('gridform');
gridform.db = db;
gridform.mongo = mongo;
var form = gridform(); // all good

The gridform function returns an instance of formidable.IncomingForm so you can process uploads without changing any code.


This module utilizes the gridfs-stream module which is exposed as require('gridform').gridfsStream.


Run the tests with make test.