Bmaed Riasbet
Bmaed Riasbet

Reputation: 14998

Uploading files to meteor server using Method call

I am trying to implement file uploads for meteor using Method call.

I am using this meteor package: https://atmospherejs.com/ostrio/files.

I have no problem on client side (I can send file in a base64 encoded format). on server side I am trying to implement this function : https://github.com/VeliovGroup/Meteor-Files/blob/master/docs/write.md

but I am getting this error.

Error during upload: TypeError: Images.write is not a function

Here is the code of my Method on server:

export const insertImage = new ValidatedMethod({
  name: 'images.insert',
  validate: new SimpleSchema({
    file: { type: String },
  }).validator(),
  run({ file }) {
    Images.write(file, {
      fileName: 'sample.png',
      type: 'image/png',
    }, function (error, fileRef) {
      if (error) {
        throw error;
      } else {
        console.log(`${fileRef.name} is successfully saved to FS. _id: ${fileRef._id}`);
      }
    });
  },
});

Upvotes: 2

Views: 1032

Answers (2)

Bmaed Riasbet
Bmaed Riasbet

Reputation: 14998

I have used this syntax:

Meteor.call('images.insert', {
  file: image
  }, (err, res) => {
  if (err) {
    console.log(`Error during upload: ${err}`);
  } else {
    console.log(`Upload successfully!`);
  }
});

Upvotes: 0

Marcelo Luiz Onhate
Marcelo Luiz Onhate

Reputation: 521

According to the lib documentation you will need to first instantiate Images with an instance of FilesCollection, similar to as following:

https://github.com/VeliovGroup/Meteor-Files#api-overview-full-api

import { FilesCollection } from 'meteor/ostrio:files';

const Images = new FilesCollection({
  collectionName: 'Images',
  allowClientCode: false, // Disallow remove files from Client
  onBeforeUpload(file) {
    // Allow upload files under 10MB, and only in png/jpg/jpeg formats
    if (file.size <= 10485760 && /png|jpg|jpeg/i.test(file.extension)) {
      return true;
    } else {
      return 'Please upload image, with size equal or less than 10MB';
    }
  }
});

For more details on the constructor parameters please refer to https://github.com/VeliovGroup/Meteor-Files/wiki/Constructor

Upvotes: 1

Related Questions