Created
March 12, 2015 11:46
-
-
Save bitomule/3ffb03704f5caa9c41fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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