3

我在服务器端使用 Ostrio/files(也称为流星文件(来自 VeliovGroup 或keenethics))将文件插入到 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 是按照 这个链接。我的 Images.write 按照我上面的第二个代码片段是按照 这个链接中的代码。简而言之,我正在关注文档,但我的服务器 Images.write 没有按预期工作。请帮忙!

4

1 回答 1

1

我找到了我的问题的答案。我将第四个参数设置为true,即proceedAfterUpload=true,并将记录插入db.fs.files 和db.fs.chunks。这是根据write 方法文档,虽然有点模糊。这是修改后的代码:

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 建议我插入缓冲区。

于 2018-07-22T10:07:51.580 回答