在meteor中上传和读/写文件

时间:2015-07-24 13:13:51

标签: meteor fs collectionfs

我正试图在流星中快速上传和阅读文件。但是我没有找到任何软件包/教程这样做,所以我尝试使用FS库,但我遇到了一些麻烦。

我的模板中有一个,以及这样的事件:

"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);
}

readTxtFile方法如下所示:

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

但是,当我上传文件时,页面会重新加载通过参数传递的文件,网址如下所示:http://localhost:3000/port?myFileUpload=textfile.txt

当代码执行第http://localhost:3000/port?myFileUpload=textfile.txt

时,页面会重新加载

即使我设置了FS.debug = true;我无法通过客户端和服务器日志看到任何错误;除了第一个日志

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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您需要以下内容以防止窗口使用默认事件处理drop事件:

application_controller.rb