0

我正在尝试使用 GridFS 下载保存在 mongoDB 中的 +200M 二进制文件。我的问题是无法开始下载。我将 nodejs 与 mongodb 和 gridfs-stream 一起使用。

在 routes.js 中:

router.get('/getlog',function(req,res) {
    if (req.isAuthenticated())
    {
        var mongo = require('mongodb');
        var Grid = require('gridfs-stream');
        var db = new mongo.Db('logApp', new mongo.Server("127.0.0.1", 27017));

        db.open(function (err) {
            if (err) 
                return handleError(err);
        });

        var gfs = Grid(db, mongo);
        var id = req.query.id;
        gfs.exist({_id: id}, function (err, found) {
            if (err) return handleError(err);
            if (!found)
                res.send('Error on the database looking for the file.')
        });

        var readStream = gfs.createReadStream({
            _id: id
        }).pipe(res);
    }
    else
        res.redirect('/login');
});

在我看来,某处:

td #[a(href="getlog?id=#{log.logId}", download="logfile") #[span(name='log').glyphicon.glyphicon-download]]

从 nodejs 日志生成响应:

GET /getlog?id=55818770b276172922b945b8 - - ms - -

但下载永远不会开始......我不知道发生了什么......

4

1 回答 1

1

改变

gfs.exist({_id: id}, function (err, found) {
            if (err) return handleError(err);
            if (!found)
                res.send('Error on the database looking for the file.')
        });

        var readStream = gfs.createReadStream({
            _id: id
        }).pipe(res);

gfs.exist({_id: id}, function (err, found) {
            if (err) return handleError(err);
            if (!found)
                return res.send('Error on the database looking for the file.');
            gfs.createReadStream({_id: id}).pipe(res);
        });

请试试。

于 2015-06-18T09:57:23.080 回答