我遇到了将代码文件写入Gridfs的代码段,但是我无法找到将字符串更新为Gridfs的写入方法。
下面的代码片段将使用path更新,但是直接字符串缓冲区呢?
var metadata = {
"path": path
};
var writestream = gfs.createWriteStream({
filename: name,
mode: 'w',
content_type: type,
metadata: metadata
});
fs.createReadStream(path).pipe(writestream);
// var buf = new Buffer("hello");
writestream.on('close', function (file) {
console.log("Gridfs created");
});
最佳答案
即使已经有一段时间了。我遇到了这个问题,并解决了它从字符串而不是文件创建流的问题。
像这样:
var writestream = gfs.createWriteStream({
filename: fileName
});
// Create stream with buffer to pipe to writestream
var s = new stream.Readable();
s.push(pic);
s.push(null); // Push null to end stream
s.pipe(writestream);
writestream.on('close', function (file) {
// Do anything with the file
cb(null, file.filename);
}).on('error', cb);
从here获得了流创意