Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor CollectionFS - Image uploading on server

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.

like image 962
StormTrooper Avatar asked Oct 31 '22 13:10

StormTrooper


1 Answers

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.

like image 192
Urigo Avatar answered Nov 08 '22 18:11

Urigo