我在服务器端使用也称为 meteor 文件(来自 VeliovGroup 或 keenethics)的 Ostrio/files 将文件插入到 db.images、db.fs.files 和 db.fs.chunks。我的代码只设法在 db.images 中插入一条记录,而在 fs.files 和 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 的 this link .我的 Images.write 按照我上面的第二个代码片段是按照中的代码 this link .简而言之,我正在按照文档进行操作,但我的服务器 Images.write 未按预期工作。请帮忙!
最佳答案
我找到了问题的答案。我将第四个参数设置为 true 即 proceedAfterUpload=true 并将记录插入到 db.fs.files 和 db.fs.chunks 中。这是根据 write method documentation , 虽然有点模糊。这是修改后的代码:
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:确保 this.bodyParams.file 是一个缓冲区,以便插入的文件不会损坏 - 感谢 @dr.dimitru 建议我插入缓冲区。
关于meteor - 如何在服务器端将文件插入图像、fs.files 和 fs.chunks -GridFS (Ostrio/files),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51440334/