【问题标题】:GridFS + NodeJS Retrieve image from mongoDBGridFS + NodeJS 从 mongoDB 检索图像
【发布时间】:2014-12-05 16:10:00
【问题描述】:

我有以下来自 mongoDB 的带有图像的 JSON,但我不知道如何显示它:

fs.chunks:

{
        "_id" : ObjectId("542e684a8a1cec178a172673"),
        "n" : 1,
        "data" : BinData(0,"2N6DSSfbCN/LLacNDYrJUQDEZgimMUwFpQGoJP0RU19Bi4PM82DjrUnKhE/P9v7P8ZveD1oDpFBM0iml9NE3WQmAvYFoG+nhD73Jm4N9b4LpylaAN5Ef+gVdgGSTAfSUwOikXoVick5pSQCkRmTCU5NT9VVfjHdAx74/ZhFRj+TIRjzlAhzkACBElzgMwGCo7tX+FYrpQLJ5KRmXiwFFwsNtHHzXiK1eu+CG1FumhGpA/qdG8CdDgD1xUHEcerMGO/eLGR9ML7ni/VjXxWzqp2j5DG2/WuKNv7xd3Kz/vr0MctJhuaBIl35YrKhdLnzqDa0uDa6bm4jz+eNyAI2hQbayGo4kVPFe8W7wFpY7qfBvnB9kbocxfZSdADDUNwYaydpT8lIcKEN9XfQJOYZvHp0El"),
        "files_id" : ObjectId("542e684a8a1cec178a172671")
}

fs.files:

{
        "_id" : ObjectId("542e65378axdeckhb0"),
        "uploadDate" : ISODate("2012-11-01"),
        "length" : 15673,
        "chunkSize" : 33222,
        "md5" : "f66e6654854a28e3672cfhds334d223b55a1"
}

需要把“数据”变成真实的图像来展示。

我正在使用 nodeJS,但我找不到使用 GridFS 从 mongoDB 检索图像的好教程。

感谢您的帮助!

【问题讨论】:

    标签: node.js mongodb mongoose gridfs gridfs-stream


    【解决方案1】:

    对不起,这个答案太晚了,但我很惊讶还没有人回答。无论如何,假设您正在运行 Express,以下代码将下载该文件。您需要将 gfs.findOne() 函数包装到 API 调用中。

    const Grid = require('gridfs-stream');
    const mime = require('mime');
    const mongoose = require('mongoose');
    
    // connect to the db, fill in the username/password/host/port/dbName.
    // Ideally the connection is set up before the main part of the app runs.
    const path = 'mongodb://username:password@host:port/dbName';
    const dbConnection = mongoose.createConnection(path);
    
    const gfs = new Grid(dbConnection.db);
    
    // then once you get the id of the file you want:
    gfs.findOne({
        _id: id
    }, (err, file) => {
        if (err) {
            // report the error
        } else {
            // detect the content type and set the appropriate response headers.
            let mimeType = file.contentType;
            if (!mimeType) {
                mimeType = mime.lookup(file.filename);
            }
            res.set({
                'Content-Type': mimeType,
                'Content-Disposition': 'attachment; filename=' + file.filename
            });
    
            const readStream = gfs.createReadStream({
                _id: id
            });
            readStream.on('error', err => {
                // report stream error
            });
            // the response will be the file itself.
            readStream.pipe(res);
        }
    });
    

    【讨论】:

    • 我收到一个错误:UnhandledPromiseRejectionWarning: TypeError: cursor.nextObject is not a function
    • @SheeceGardazi 你能链接到你的代码吗?如果没有,我建议将您的问题作为一个单独的问题发布。
    • 添加这个:eval(Grid.prototype.findOne = ${Grid.prototype.findOne.toString().replace('nextObject', 'next')});解决了我的问题,我在这里添加了解决方案stackoverflow.com/a/62241037/4935275
    • 我可能会在接下来的几周内更新我的答案,因为 MongoDB 有他们的官方GridStore API,并且 gridfs-stream 的所有者已经停止开发。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2021-08-29
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    相关资源
    最近更新 更多