Passing binary file data through Meteor.call and saving to filesystem

807 views
Skip to first unread message

Joshua Ellis

unread,
Mar 5, 2015, 7:19:25 PM3/5/15
to meteo...@googlegroups.com
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?


Avital Oliver

unread,
Mar 5, 2015, 7:41:42 PM3/5/15
to meteo...@googlegroups.com
We're moving the forums to http://forums.meteor.com, so it's better to ask there. (This group will be closed in a few days)



--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/meteor-talk/6088d758-cf03-4021-92e8-b3f97423970e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages