node package manager


Stream formidable uploads into MongoDB GridFS



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.