Upload File Client / Server issues

i am creating a file upload page in this way Save files in Meteor · GitHub , i am having problem Error invoking Method ‘saveFile’


my code : client_savefiles :

import {Meteor} from 'meteor/meteor';
/**
 * @blob (https://developer.mozilla.org/en-US/docs/DOM/Blob)
 * @name the file's name
 * @type the file's type: binary, text (https://developer.mozilla.org/en-US/docs/DOM/FileReader#Methods) 
 *
 * TODO Support other encodings: https://developer.mozilla.org/en-US/docs/DOM/FileReader#Methods
 * ArrayBuffer / DataURL (base64)
 */
if (Meteor.isClient) {
 Meteor.saveFile = function(blob, name, path, type, callback) {
  var fileReader = new FileReader(),
      method, encoding = 'binary', type = type || 'binary';
    switch (type) {
      case 'text':
        // TODO Is this needed? If we're uploading content from file, yes, but if it's from an input/textarea I think not...
        method = 'readAsText';
        encoding = 'utf8';
        break;
      case 'binary': 
        method = 'readAsBinaryString';
        encoding = 'binary';
        break;
      default:
        method = 'readAsBinaryString';
        encoding = 'binary';
        break;
    }
    console.log(fileReader);
    fileReader.onload = function(file) {
      Meteor.call('saveFile', file.target.result, name, path, encoding, callback);
    }
    fileReader[method](blob);
  }
}

server_savefiles

import {fs} from 'fs';
import {Meteor} from 'meteor/meteor';
import { check } from 'meteor/check';

if (Meteor.isClient) {
  Meteor.methods({
    'saveFile': function(blob, name, path, encoding) {
      check(blob, Match.Any);
      check(name, String);
      check(path, String);
      check(encoding, String);
      var path = cleanPath(path),
        name = cleanName(name || 'file'), encoding = encoding || 'binary',
        chroot = Meteor.chroot || '../../../../public';
        path = chroot + (path ? '/' + path + '/' : '/');
      
      // TODO Add file existance checks, etc...
      fs.writeFile(path + name, blob, encoding, function(err) {
        if (err) {
          throw (new Meteor.Error(500, 'Failed to save file.', err));
        } else {
          console.log('The file ' + name + ' (' + encoding + ') was saved to ' + path);
        }
      }); 

      function cleanPath(str) {
        if (str) {
          return str.replace(/\.\./g,'').replace(/\/+/g,'').
            replace(/^\/+/,'').replace(/\/+$/,'');
        }
      }
      function cleanName(str) {
        return str.replace(/\.\./g,'').replace(/\//g,'');
      }
    },
  
  });
}



I think you should remove the if (Meteor.isClient) { check on the server.

this is the result when i remove if (Meteor.isClient) {

Now you need to read the error in the server console. The message on the client doesn’t help much

i don’t know why this error occurs Error invoking Method ‘saveFile’, I’m a newbie

Unfortunately I don’t know either.

If this is a server file, you will never have Meteor.isClient on your server.

It would be best to adopt the recommended folder structure for Node/Meteor and keep client files in client folders and server files in server folders. This way you can avoid writing “Meteor. isSomething” all the time.

On another note, what is your use case? Do you need to save many files? In this case you should upload to a CDN and link your files from the CDN.

Screen Shot 2022-04-15 at 7.23.20 PM

How did you start your meteor app? Go to where you start the app and that should display the error that happened for “Internal Server Error”

You need to register the meteor methods server side. import the file in your server index, then it should work.

I already registered in the server directory : server_save_files