Meteor CollectionFS - Uploading Images to the Server

I am using Meteor CollectionFS. Currently, my file upload is done on the client. I want to upload files on a server so that other platforms such as andriod or ios can use my file upload services.

Now 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 can see, everything is done in the client controller. How can I do this on server controllers in Meteor?

How to send a file to the server so that I can process, insert or upload my images there?

EDIT

As you know, an Android app will send a base64 encoded string. So how will I relate to this here? I want to have a centralized function for uploading images to Meteor Server.

+5
source share
1 answer

You can put this logic into the meteor method . Then you can decide whether you want this method to run only on the server or on the client and on 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 that can run on the server or client and server - more about the structure of Meteor files 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 the server and client. I would also like to learn more about meteor techniques in the Meteor Guide .

+2
source

Source: https://habr.com/ru/post/1238262/


All Articles