FileReader API, Meteor.call() and uploading files

Man, this is driving me crazy. I’m trying to use the HTML5 FileReader API to allow uploads of MP3s to a server – I can’t store them as GridFS as I’m doing some other stuff to them via command line, etc. I’m using the reader.readAsText() function of the FileReader API, which encodes the uploaded data as UTF-8 by default, wthen doing a Meteor.call() to pass the file (as a string) to the server, which then reads the string, creates a Buffer, and writes a file, like so:

Client side:

// this is triggered by a change event

file = $(e.currentTarget)[0].files[0];
 var reader = new FileReader();


 reader.onload = function(e){
 Meteor.call('processMedia', e.target.result, function (error, result) {
if(err) throw err;
console.log(result});
 }


 reader.readAsText(file);

Server side:

fs = Npm.require('fs');


Meteor.methods({


'processMedia' : function(data){


buffer = new Buffer(data, 'utf-8');
filename = "/tmp/" + new Date().getTime() + '.mp3';
fs.writeFile(filename, buffer, function (err) {
  if (err) throw err;
  return "It's saved!";
});
}


});

So here’s the maddening part: it works, the file is saved. When I open it, the MP3 player reads the metadata correctly, but the file won’t play. So I tried doing JPGs instead of MP3s, to see what that did, and they’re also broken.

I know I can’t send an ArrayBuffer through Meteor.call(), so my options seem to be text or binary string (which I don’t want to do, as it’s deprecated).

So what am I missing here? It’s driving me nuts. :slight_smile: Anybody got any ideas?

Hi.
You can send any binaries or compatible ArrayBuffer with EJSON.
I am working with uploading files with ArrayBuffer, Uint8Array, BinaryString and Base64String - and it works fine. Moreover if you can read file’s metadata it seems like all works great.
Could you compare ArrayBuffers of the same file on a client an a server (NOT Buffer but ArrayBuffer)?

Hey @jzellis,

I’m working with peerlibrary:meteor-file to send files with a Meteor.call to the server. However, it doesn’t work for files with more than some 10 KiB. How big have been the files that you could send to the server this way? Did you work with chunks?