File that was uploded on server-side using write method is corrupt. How can I fix issue?
See original GitHub issue- ostrio:files@1.9.11
 - METEOR@1.7.0.3
 - Fedora 26
 - Server
 
I am inserting a file into db.images, db.fs.files and db.fs.chunks using meteor-files or Ostrio-files (from VeliovGroup or keenethics). I am storing the files on GridFS. I installed gridfs-stream. I already have a meteor client app code that reads files from meteor-files and display them via a browser - this is how I view my newly inserted files, server-side. My server-side code for inserting files is as follows:
Images.write(this.bodyParams.file, {
  fileName: 'SignUpTermsAndConditions_' + this.bodyParams.name,
  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);
I am able to insert a file client-side properly and when I view that file, the file is not corrupt. My client-side code for inserting into db.images, db.fs.files and db.fs.chunks is as follows:
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'
});
If you want me to provide the code that reads the file from GridFS client-side let me know.
As stated above my issue is that my server-side code for inserting a file, results in a corrupt file. Please help.
For your info: I am passing the file in a REST api post; I am passing the file in the body of the post.
For your info this is the error I get when trying to open the file in LibreOffice:
The file 'nSQGqasoSy6JF3DkJ-2.odt' is corrupt and therefore cannot be opened. LibreOffice can try to repair the file.
The corruption could be the result of document manipulation or of structural document damage due to data transmission.
We recommend that you do not trust the content of the repaired document.
Execution of macros is disabled for this document.
Should LibreOffice repair the file?
For your info I am reading the file in node.js using fs.readFile method. I have also tried reading the file locally within meteor.js instead but still results in a corrupt file. Here is my node.js code for fs.readFile and post
	fs.readFile('./Statement.odt', function read(err, data) { //where fs = require('fs')
		if (err) {
			throw err;
		}
		var file = data;
		
		request.post({
		  uri: url, 
		  headers:	 {
			'X-User-Id': userId,
			'X-Auth-Token': authToken
		  },
		  form: {
			  file: file,
			  name:"Statement.odt"
		  }
		}, function(err, httpResponse, body) {
		  if (err) {
			return console.error('post failed:', err);
		  }
		
		  console.log('Get successful!  Server responded with:', body);
		});
	});
For your info: I have tried reading a pdf document instead of a LibreOffice document but also results in a corrupt file. Please help!
Issue Analytics
- State:
 - Created 5 years ago
 - Comments:19 (8 by maintainers)
 

Top Related StackOverflow Question
You are correct. Silly of me.
And thanks for nudging me in the right direction.