Search code examples
javascriptmeteorfswritefile

What cause the unknown encoding error in my writeFile?


I'm trying to send a file from my client to the server and write it in a directory.

I can send the file but I can't write it because of this error:

I20170518-07:09:54.810(2)? Exception while invoking method 'file-upload' Error: Unknown encoding:
I20170518-07:09:54.810(2)? hi i am the content of the file
I20170518-07:09:54.810(2)?     at assertEncoding (fs.js:88:11)
I20170518-07:09:54.811(2)?     at Object.fs.writeFileSync (fs.js:1153:3)
I20170518-07:09:54.811(2)?     at [object Object].fileUpload (server/helpers_docker.js:349:10)
I20170518-07:09:54.811(2)?     at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1711:12)
I20170518-07:09:54.811(2)?     at packages/ddp-server/livedata_server.js:711:19
I20170518-07:09:54.811(2)?     at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20170518-07:09:54.811(2)?     at packages/ddp-server/livedata_server.js:709:40
I20170518-07:09:54.812(2)?     at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20170518-07:09:54.812(2)?     at packages/ddp-server/livedata_server.js:707:46
I20170518-07:09:54.812(2)?     at Session.method (packages/ddp-server/livedata_server.js:681:23)
I20170518-07:09:54.812(2)?     at packages/ddp-server/livedata_server.js:551:43

So I understand that the encoding is unknown but I specified it.

Here it's the client:

'change .file-upload-input': function(event, template){
   var func = this;
   var file = event.currentTarget.files[0];
   var reader = new FileReader();
   reader.onload = function(fileLoadEvent) {
      Meteor.call('file-upload', file.name, reader.result);
      console.log(file);
   };
   reader.readAsBinaryString(file);
 },

Here it's the server (where I specified the encoding):

'file-upload': function (fileInfo, fileData) {
      fs.writeFileSync(process.env.HOME + '/compose/area/',fileInfo, fileData,'utf8');
}

Any help will be appreciated


Solution

  • According to docs, writeFileSync signature is fs.writeFileSync(file, data[, options]). So you should change your code like:

    var data = composeSomehow(fileInfo, fileData);
    fs.writeFileSync(process.env.HOME + '/compose/area/', data, 'utf8');
    

    Here composeSomehow should combine together all data your need and return one of data <string> | <Buffer> | <Uint8Array>