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,'');
}
},
});
}