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. Anybody got any ideas?