简体   繁体   中英

How to download a file from mongodb GridFS collection?

I'm connecting with mongodb this way:

routes.js :

"use strict"

const Router = require("koa-router")
const bind = require("koa-clean")

const FsFilesController   = require ("./controllers/fsfiles-controller")
    
const fsfiles = new Router({ prefix: "/fsfiles" })
    .post  ("/get",  bind (FsFilesController.get))

const root = new Router({ prefix: "/api" })
    .use  (fsfiles.routes())

module.exports = [ root ]

fsfiles-controller :

"use strict"

const Vars  = require ("../models/fsfiles")

const get = async ({ db }, { filename, sub_key }) => {
    
    const variable = await Vars.get (db, filename)
    console.log("variable: ", variable)

    //if (variable && variable[sub_key])
    //    return variable[sub_key]

    if (variable)
        return variable
    else
        return [ 404, "not found" ]        
}

module.exports = { get }

fsfiles.js :

"use strict";

const __MODULE__ = "fs.files"

const get = (db, filename) =>
    db.collection(__MODULE__).findOne({ filename })

module.exports =
    { get }

My MongoDB collections: https://i.imgur.com/ZfY3sAZ.png

I'm requesting the data from a c++ project using curl as:

auto data = Request("https://....herokuapp.com/api/fsfiles/get"
, "POST"
, R"({"filename": "test.txt", "sub_key": ""})");

However, this is what being downloaded to data :

{"_id":"632e4cc00f702f1908c66d83","length":3,"chunkSize":261120,"uploadDate":"2022-09-24T00:18:09.361Z","filename":"test.txt","metadata":{}}

How do i download the content of the test.txt file?

Use the mongo-c-driver or mongo-cxx-driver library. See the official GridFS C sample or C++ sample .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM