我在服务器端使用Ostrio/files,也称为流星文件(来自VeliovGroup或keenrethics(,将文件插入db.images、db.fs.files和db.fs.chunks
Images.write(this.bodyParams.file, {
fileName: 'SignUpTermsAndConditions_' + this.bodyParams.name,
fielId: 'abc123myId', //optional
type: 'application/vnd.oasis.opendocument.text',
meta: {owner: this.userId,createdAt: new Date()}
}, function (error, fileRef) {
if (error) {
throw error;
} else {
console.log(fileRef.name + ' is successfully saved to FS. _id: ' + fileRef._id);
}
});
我可以使用以下代码将客户端的文件插入到db.images、db.fs.files和db.fs.chunks中:
Images.insert({
file: file, // where var file = e.currentTarget.files[0];
fileName: fileName + e.currentTarget.files[0].name,
onStart: function () {
},
onUploaded: function (error, fileObj) {
if (error) {
alert('Error during upload: ' + error);
} else {
}
},
streams: 'dynamic',
chunkSize: 'dynamic'
});
上面第一个片段中的服务器代码工作不正常或不符合预期。请帮忙。
有关信息:我如何设置我的gridFS是根据这个链接。我的Images.write根据我上面的第二个代码片段是根据中的代码这个链接。简而言之,我正在遵循文档,但我的服务器Images.write没有按预期工作。请帮忙!
我找到了问题的答案。我将第四个参数设置为true,即processedAfterUpload=true,并将记录插入到db.fs.files和db.fs.chunks中。这是根据编写方法文档,但有点模糊。这是修改后的代码:
Images.write(this.bodyParams.file, {
fileName: 'SignUpTermsAndConditions_' + this.bodyParams.name,
fielId: 'abc123myId', //optional
type: 'application/vnd.oasis.opendocument.text',
meta: {owner: this.userId,createdAt: new Date()}
}, function (error, fileRef) {
if (error) {
throw error;
} else {
console.log(fileRef.name + ' is successfully saved to FS. _id: ' + fileRef._id);
}
},proceedAfterUpload=true);
就是这样。
PS:请确保.bodyParams.file是一个缓冲区,这样插入的文件就不会损坏-感谢@dr.dimitru建议我插入缓冲区。