How to use GridFS to store images using Node.js and Mongoose

前端 未结 4 1552
刺人心
刺人心 2020-12-14 07:59

I am new to Node.js. Can anyone provide me an example of how to use GridFS for storing and retrieving binary data, such as images, using Node.js and Mongoose? Do I need to d

4条回答
  •  误落风尘
    2020-12-14 08:11

    I suggest taking a look at this question: Problem with MongoDB GridFS Saving Files with Node.JS

    Copied example from the answer (credit goes to christkv):

    // You can use an object id as well as filename now
    var gs = new mongodb.GridStore(this.db, filename, "w", {
      "chunk_size": 1024*4,
      metadata: {
        hashpath:gridfs_name,
        hash:hash,
        name: name
      }
    });
    
    gs.open(function(err,store) {
      // Write data and automatically close on finished write
      gs.writeBuffer(data, true, function(err,chunk) {
        // Each file has an md5 in the file structure
        cb(err,hash,chunk);
      });
    });
    

提交回复
热议问题