meteorgridfsgridfs-stream

How to insert a file, server-side, into images, fs.files and fs.chunks -GridFS (Ostrio/files)


I am using Ostrio/files also known as meteor-files (from VeliovGroup or keenethics) on the server-side to insert a file to db.images, db.fs.files and db.fs.chunks. My code only manages to insert a record only in db.images and nothing on fs.files and fs.chunks. My code is as follows:

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);
  }
});

I am able to insert a file from the client into db.images, db.fs.files and db.fs.chunks using the following code:

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'
});

The server code as per the first snippet above is one that is not working correctly or as expected.Please help.

For info: how I setup my gridFS is as per this link. My Images.write as per my second code snippet above is as per the code in this link. In short I am following the documentation but my server Images.write is not working as expected.Please help!


Solution

  • I found the answer to my question. I set the fourth parameter to true i.e proceedAfterUpload=true and records are inserted in db.fs.files and db.fs.chunks. This is as per the write method documentation, it is slightly vague though. Here is the amended code:

    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);

    That's it.

    PS: Make sure this.bodyParams.file is a buffer so that the inserted file is not corrupt - thanks to @dr.dimitru for advising me on inserting a buffer.