I am using Meteor CollectionFS. Currently my file uploading is performed on client. I want to perform file uploading on server so that other platforms like andriod or ios can use my services of file uploading.
Currently here is my code:
client.html
<input type="file" custom-on-change="uploadFile">
clientController.js
app.controller('clientController', function ($scope, $meteor, $filter) {
$scope.uploadFile = function(event){
var files = event.target.files;
for (var i = 0, ln = files.length; i < ln; i++) {
files[i].userId = Meteor.userId();
Images.insert(files[i], function (err, fileObj) {
});
}
};
});
app.directive('customOnChange', function() {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var onChangeHandler = scope.$eval(attrs.customOnChange);
element.bind('change', onChangeHandler);
}
};
});
Schema.js
Images = new FS.Collection("images", {
stores: [
new FS.Store.FileSystem("images", {path: '~/uploads'})
]
});
The code works perfect for me. But as you see everything is done in the client controller. How can I perform this on server controllers in Meteor?
How can I send my file to the server so that I can process, insert or upload my images there?
EDIT
As you know that an Android App will be sending a base64 encoded string. So how will I treat that here? I want to have a centralized function for Image Uploading on Meteor Server.
You can place that logic inside a Meteor Method. Then you can decide if you want that method to run only on the server or both on the client and the server (latency compensation).
So I would change your controller to:
$scope.uploadFile = function(event){
Meteor.call("uploadFiles", event.target.files);
};
Schema.js (or any other file which can run on server or client and server - read more about Meteor file structure here)
Images = new FS.Collection("images", {
stores: [
new FS.Store.FileSystem("images", {path: '~/uploads'})
]
});
Meteor.methods({
uploadFiles: function (files) {
for (var i = 0, ln = files.length; i < ln; i++) {
files[i].userId = Meteor.userId();
Images.insert(files[i], function (err, fileObj) {
});
}
}
});
The method can also return values, run on server and client. I would also read more about Meteor Methods on the Meteor guide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With