How can I stream file uploads directly into Mongo's GridFS using Formidable? How can I stream file uploads directly into Mongo's GridFS using Formidable? mongodb mongodb

How can I stream file uploads directly into Mongo's GridFS using Formidable?


Since opening a GridStore file is async and formidable is not, you'll need to do some manual buffering of formidables incoming stream while waiting for the GridStore file to open. Since GridFS cannot be piped to you'll then need to manually write each buffered chunk to the GridStore after it opens.

Just released gridform which should do what you need.

https://github.com/aheckmann/gridform


You could move your form's handling code into the store.open callback, for example :

function upload (request, response, next, options) {    var store = new Mongo.GridStore(options.mongoose.connection.db, new ObjectID, 'w+', {        root: 'store',        chunk_size: 1024 * 64    } );    store.open( function (error, store) {        var form = new Formidable.IncomingForm();        form.onPart = function (part) {            if(!part.filename){                form.handlePart(part);                return;            }            part.on('data', function(buffer){                store.write(buffer);            });            part.on('end', function() {                store.close();            });        };        form.parse(request);    });    response.send();}