Problem with MongoDB GridFS Saving Files with Node.JS

倾然丶 夕夏残阳落幕 提交于 2019-11-28 10:35:05

There are a couple of solutions. You can use writeBuffer, writeFile or the new simple grid class. Under is your example adjusted for the fact of using a buffer instance.

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

In general the best place to start are the tests that cover a wide usage profile for the gridfs classes. Look at.

https://github.com/christkv/node-mongodb-native/tree/master/test/gridstore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!