Meteor文件上传不起作用

Ima*_*ens 2 upload meteor

我已将软件包cfs:standard-packages和cfs:filesystem添加到我的meteor项目中.我想使用带有此输入的表单为我的博客上传精选图像.

<div class="form-group">
        <label for="featuredImage">Featured Image</label>
        <input type="file" id="fImage" required>
        <p class="help-block">Please choose an image file.</p>
</div>
Run Code Online (Sandbox Code Playgroud)

和事件javascript

Template.AddPost.events({
    'change #fImage': function(event, template) {

        var image = template.find('[id=fImage]').value;
        var lastIndex = image.lastIndexOf("\\");
        if (lastIndex >= 0) {
            image = image.substring(lastIndex + 1);
        }
        if (!image.match(/\.(jpg|jpeg|png|gif)$/)) {
            alert("not an image");
        } else {
            FS.Utility.eachFile(event, function(file) {
                var fileObj = new FS.File(file);
                Meteor.call('uploadFeaturedImage', fileObj);
            });
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

服务器上的'uploadFeaturedImage'方法是

Meteor.methods({

    'uploadFeaturedImage': function(fileObj){
        Uploads.insert(fileObj, function(err){
            console.log(err);
        });   
    }
});
Run Code Online (Sandbox Code Playgroud)

当我选择要上传的图像文件时,我收到此错误 - "调用方法时出现异常'uploadFeaturedImage'错误:DataMan构造函数接收到它不支持的数据"

任何人都有任何想法为什么会这样?谢谢.

cha*_*hne 5

我从collectionFS文档中复制了一些解释,因为它在那里描述得非常好.

当您需要插入位于客户端上的文件时,请始终在客户端上调用myFSCollection.insert.虽然您可以定义自己的方法,将其传递给fsFile,并在服务器上调用myFSCollection.insert,但困难在于将数据从客户端传递到服务器.将fsFile传递给方法时,仅发送文件信息而不是数据.相比之下,当您直接在客户端上执行插入操作时,它会在插入后自动对文件的数据进行分块,然后将其排队以便按块发送到服务器.然后就是重新组合服务器上的所有块并将数据填充回fsFile.因此,进行客户端插入实际上可以为您节省所有这些复杂的工作,这就是我们推荐它的原因.

看看这里

所以你的方法不起作用,因为没有数据发送到服务器.