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 itgridformdb = db;gridformmongo = mongo;// in your http servervar app = httpServer// create a gridformvar form = gridform;// returns a custom IncomingFormassertform instanceof formidableIncomingForm;// optionally store per-file metadataformon'fileBegin'filemetadata = 'so meta'// parse normallyformparsereq// use files and fields as you do todayvar file = filesupload;filename // the uploaded file namefiletype // file type per [mime]()filesize // uploaded file size (file length in GridFS) named "size" for compatibilityfilepath // same as file.name. included for compatibilityfilelastModified // included for compatibility// files contain additional gridfs infofileroot // the root of the files collection used in MongoDB ('fs' here means the full collection in mongo is named 'fs.files')fileid // 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 = gridformoptions;
filename function is passed the
file.name before streaming to MongoDB providing an opportunity to return a customized filename with a prefix etc.
mongo are required unless you've specified them on
var gridform = require'gridform';gridformdb = db;gridformmongo = 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
Run the tests with