Encoding Issue with Gridfs-stream, Multiparty Middleware

I am having some problems properly storing and retrieving files with GridFS. Currently, if I submit a .txt file I end up getting the contents of the file back, but if i submit a .doc file, I get a bunch of gibberish (like blackdiamonds with question marks in it).

My end goal is just to be able to submit the file and then allow someone to download the file later on a different request.

Writing Code:

router.post('/jobs/listing/:job/apply', multipartyMiddleware, function(req, res, next){
  var myFile = req.files.file;
  var conn = mongoose.createConnection('mongodb://localhost/test');
  conn.once('open', function () {

    var gfs = Grid(conn.db, mongoose.mongo);
    var readfile = fs.createReadStream(myFile.path);
    var f = readfile.pipe(gfs.createWriteStream({
        filename: myFile.name
    f.on('close', function(){
        console.log('File Added to GRIDFS');

Reading Code:

var conn = mongoose.createConnection('mongodb://localhost/test');
conn.once('open', function () {
        var gfs = Grid(conn.db, mongoose.mongo);

        var readstream = gfs.createReadStream({
            filename: req.file //set to desired filename

        var f = readstream.pipe(res);

Any suggestions? I would really appreciate any help you can provide. Thanks.

Edit: Problem had to do with an uploading issue in angular.


Here's a simple implementation that I copied from another developer and modified. This is working for me:


var Busboy = require('busboy'); // 0.2.9
var express = require('express'); // 4.12.3
var mongo = require('mongodb'); // 2.0.31
var Grid = require('gridfs-stream'); // 1.1.1"
var app = express();
var server = app.listen(9002);

var db = new mongo.Db('test', new mongo.Server('', 27017));
var gfs;
db.open(function(err, db) {
  if (err) throw err;
  gfs = Grid(db, mongo);

app.post('/file', function(req, res) {
  var busboy = new Busboy({ headers : req.headers });
  var fileId = new mongo.ObjectId();

  busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
    console.log('got file', filename, mimetype, encoding);
    var writeStream = gfs.createWriteStream({
      _id: fileId,
      filename: filename,
      mode: 'w',
      content_type: mimetype,
  }).on('finish', function() {
    // show a link to the uploaded file
    res.writeHead(200, {'content-type': 'text/html'});
    res.end('<a href="/file/' + fileId.toString() + '">download file</a>');


app.get('/', function(req, res) {
  // show a file upload form
  res.writeHead(200, {'content-type': 'text/html'});
    '<form action="/file" enctype="multipart/form-data" method="post">'+
    '<input type="file" name="file"><br>'+
    '<input type="submit" value="Upload">'+

app.get('/file/:id', function(req, res) {
  gfs.findOne({ _id: req.params.id }, function (err, file) {
    if (err) return res.status(400).send(err);
    if (!file) return res.status(404).send('');

    res.set('Content-Type', file.contentType);
    res.set('Content-Disposition', 'attachment; filename="' + file.filename + '"');

    var readstream = gfs.createReadStream({
      _id: file._id

    readstream.on("error", function(err) {
      console.log("Got error while processing stream " + err.message);


For anyone who ends up with this issue, I had the same symptoms and the issue was a middleware. This was a gnarly bug. The response was being corrupted by connect-livereload.

Github Issue on busboy

Github Issue on gridfs stream

My response on a Similar Stack Overflow Issue

Need Your Help

Java: using switch statement with enum under subclass

java enums switch-statement

First I'll state that I'm much more familiar with enums in C# and it seems like enums in java is a quite mess.

using flash animations in cocos2dx project

c++ flash animation cocos2d-x

I am currently using sprite animation in my cocos2dx game using plist.