2

I'm trying to upload and read a file on the fly in meteor. However I didn't find any packages/tutorials to do this so I tried to use the FS library but I have some troubles with it.

I have a in my template, and an event like this :

"change .myFileUpload": function(e, tmpl) {
    e.preventDefault();
    var fileInput = tmpl.find('input[type=file]');

    console.log('test');
    // grab a list of the files selected with the file chooser
    // input
    debugger;
    console.log(fileInput);
    var theFile = new FS.File(fileInput);
    console.log(theFile);
    var rose = JSON.parse(Assets.getText(fileInput));
    Meteor.call('readTxtFile',fileInput);
    Meteor.call('readTxtFile',theFile);
}

The method readTxtFile looks like this :

    readTxtFile : function(file){
    console.log(file);
    fs.readFile(file, function(err,data){
        if(err){
            throw new Error("Fail read");
        }else{
            console.log(data);
        }
    });

But, when I upload a file, the page is reloaded with the file passed through arguments, the url looks like this : http://localhost:3000/port?myFileUpload=textfile.txt

The page is reloaded when the code execute the line http://localhost:3000/port?myFileUpload=textfile.txt

Even if I set a FS.debug = true; I can't see any error, neither through the client nor the server log; except the first log

console.log(fileInput);
> <input type=​"file" class=​"myFileUpload">​          portTmpl.js:12 

Any thoughts ?

TLR
  • 577
  • 3
  • 8
  • 24
  • most people use the collectionfs package: https://github.com/CollectionFS/Meteor-CollectionFS – Christian Fritz Jul 24 '15 at 14:58
  • If you're think CollectionFS is too complex, then try this one: https://github.com/VeliovGroup/Meteor-Files/ – dr.dimitru Jul 24 '15 at 15:42
  • 1
    I can use the collection FS package but I don't see any method to read a file uploaded. And I don't get why I should use a collection since I just want to write and read files ? – TLR Jul 27 '15 at 08:05

1 Answers1

0

You need the following to prevent the window from handling the drop event with the default event:

window.addEventListener("drop",function(ev){
  ev = ev || event;
  ev.preventDefault();
},false);
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39