Skip to content Skip to sidebar Skip to footer

Unable To Write Buffer To Mongodb Gridfs

Hi guys i have spent almost a day trying to figure this one out. I'm using multer's inMemory flag to upload an image from my webpage. The obvious attempt is to write the buffer rec

Solution 1:

Now, what you missed here is that the "buffer" from the "inMemory" option is not "either/or" and does not mean that the content is held "In Memory" instead. It is in fact a "copy" of the data that is also sent to the temporary file on disk.

So it really doesn't matter if you set "inMemory" or not as the files will still be created ( by default in the /tmp directory ) but these will of course unlink when out of scope:

varasync = require('async'),
    express = require('express'),
    multer = require('multer'),
    fs = require('fs'),
    mongoose = require('mongoose'),
    Grid = require('gridfs-stream'),
    Schema = mongoose.Schema;

Grid.mongo = mongoose.mongo;
var app = express(),
    gfs = {};

// Set up multer middleware
app.use(
  multer({
    //inMemory: true
  })
);

// Register handler
app.post('/',function (req,res) {

  async.eachLimit(Object.keys(req.files), 10, function(file,callback) {
    var fileobj = req.files[file];

    var writeStream = gfs.createWriteStream({
      "filename": fileobj.fieldname
    });

    fs.createReadStream(fileobj.path).pipe(writeStream);

    writeStream.on('close',function() {
      console.log('done');
      callback();
    });

    writeStream.on('error',callback);

  },function(err) {
    if (err) {
      console.log(err);
      res.status(500).end();
    }
    res.status(200).end();
  });

});

mongoose.connect('mongodb://localhost/test');


// Start app listen and eventsvar server = app.listen(3000,function() {

  mongoose.connection.on('open',function(err) {
    if (err) throw err;
    // Set up connection
    gfs = Grid(mongoose.connection.db);
    console.log('listening and connected');
  });

});

And of course a simple test:

varFormData = require('form-data'),
    fs = require('fs'),
    http = require('http');

var fname  = 'GearsLogo.png';
var form = newFormData();
form.append(fname,fs.createReadStream(fname))

var request = http.request({
  method: 'post',
  port: 3000,
  headers: form.getHeaders()
});

form.pipe(request);

request.on('response',function(res) {
  console.log(res.statusCode);
});

Alternately call the middle-ware in-line with your request method, and/or set up the onFileUploadComplete() handler rather than iterate the content of req.files. The "gridfs=stream" package is probably the simplest option you have to upload content and trying to work from a buffer that is a copy is not really going to offer any real advantage since the IO cost and storage is always going to be there.

Post a Comment for "Unable To Write Buffer To Mongodb Gridfs"