如何在服务器端将文件插入图像,fs.files和fs.chunks -GridFS(Ostrio /文件)

时间:2018-07-20 10:18:21

标签: meteor gridfs gridfs-stream

我在服务器端使用Ostrio /文件(也称为流星文件(来自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是按照 this link。根据上面的第二个代码段,我的Images.write是根据中的代码 this link。简而言之,我正在遵循文档,但是我的服务器Images.write无法正常工作。请帮忙!

1 个答案:

答案 0 :(得分:1)

我找到了问题的答案。我将第四个参数设置为true,即procedAfterUpload = 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在插入缓冲区方面为我提供建议。

相关问题