Gridfs-stream、多方中间件的编码问题

Encoding Issue with Gridfs-stream, Multiparty Middleware

我在使用 GridFS 正确存储和检索文件时遇到了一些问题。目前,如果我提交一个 .txt 文件,我最终会得到文件的内容,但是如果我提交一个 .doc 文件,我会得到一堆乱码(比如带有问号的 blackdiamonds)。

我的最终目标只是能够提交文件,然后允许其他人稍后根据不同的请求下载文件。

编写代码:

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');
        res.end();
    });
  });
}

阅读代码:

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);
});

有什么建议吗?如果您能提供任何帮助,我将不胜感激。谢谢

编辑:问题与 angular 中的上传问题有关。

这是我从其他开发人员那里复制并修改的一个简单实现。这对我有用:

https://gist.github.com/pos1tron/094ac862c9d116096572

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('127.0.0.1', 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,
    });
    file.pipe(writeStream);
  }).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>');
  });

  req.pipe(busboy);
});

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

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);
      res.end();
    });

    readstream.pipe(res);
  });
});

对于最终遇到此问题的任何人, 我有同样的症状,问题是中间件。这是一个棘手的错误。响应被 connect-livereload 损坏。

Github Issue on busboy

Github Issue on gridfs stream