Skip to content

Instantly share code, notes, and snippets.

@bitomule
Created March 12, 2015 11:46
Show Gist options
  • Save bitomule/3ffb03704f5caa9c41fe to your computer and use it in GitHub Desktop.
Save bitomule/3ffb03704f5caa9c41fe to your computer and use it in GitHub Desktop.
Router.route('/uploadFile', {name:'uploadFile', where: 'server' })
.post(function(){
var request = this.request;
var response = this.response;
var buffers = [];
var totalLength = 0;
var fileUpload = Meteor.bindEnvironment(function(file){
Files.insert(file);
}, function(e) {
throw e
});
request.on('data', function(chunk) {
buffers.push(chunk);
totalLength += chunk.length;
})
request.on('end', function() {
if(request.headers["content-length"] == totalLength){
var data = Buffer.concat(buffers);
var newFile = new FS.File();
newFile.name(request.headers["filedata-name"]);
newFile.attachData(data, {type: request.headers["filedata-type"]}, function (error) {
if (error) throw error;
var savedFile = fileUpload(newFile);
response.writeHead(200, {"Content-Type": "text/html"});
response.end();
});
}else{
throw new Meteor.error("Bad file size");
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment