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

Binding a jquery value to a specific DIVs height

javascript jquery html css skrollr

Sorry, I'm a jquery/js apprentice. I have a jquery sticky nav setup with skrollr set to "stick" at a top offset of 590px. This seemed okay but I came to find I need that offset to be unique on some

Design & Coding - top to bottom or bottom to top?

design coding-style

When coding, what in your experience is a better approach?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.