流星文件上传不起作用 [英] Meteor File Upload Not Working

查看:55
本文介绍了流星文件上传不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将包 cfs:standard-packages 和 cfs:filesystem 添加到我的流星项目中.我想使用带有此输入的表单为我的博客上传特色图片.

<label for="featuredImage">特色图片</label><input type="file" id="fImage" required><p class="help-block">请选择图片文件.</p>

和事件 javascript

Template.AddPost.events({'改变#fImage':函数(事件,模板){var image = template.find('[id=fImage]').value;var lastIndex = image.lastIndexOf("\\");如果 (lastIndex >= 0) {image = image.substring(lastIndex + 1);}if (!image.match(/\.(jpg|jpeg|png|gif)$/)) {alert("不是图片");} 别的 {FS.Utility.eachFile(事件,函数(文件){var fileObj = new FS.File(file);Meteor.call('uploadFeaturedImage', fileObj);});}}});

服务器上的'uploadFeaturedImage'方法是

Meteor.methods({'uploadFeaturedImage':函数(fileObj){Uploads.insert(fileObj,函数(错误){控制台日志(错误);});}});

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

有人知道为什么会发生这种情况吗?谢谢你.

解决方案

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

<块引用>

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

看看这里

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

I've added the packages cfs:standard-packages and cfs:filesystem to my meteor project. I want to upload featured images for my blog using a form with this input.

<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>

And the event 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);
            });
        }
    }
});

The 'uploadFeaturedImage' method on the server is

Meteor.methods({

    'uploadFeaturedImage': function(fileObj){
        Uploads.insert(fileObj, function(err){
            console.log(err);
        });   
    }
});

When i choose an image file to upload i get this error - "Exception while invoking method 'uploadFeaturedImage' Error: DataMan constructor received data that it doesn't support"

Anyone have any ideas why this is happening? Thank you.

解决方案

I copied some explanation from the collectionFS documentation because it is really good described there.

When you need to insert a file that's located on a client, always call myFSCollection.insert on the client. While you could define your own method, pass it the fsFile, and call myFSCollection.insert on the server, the difficulty is with getting the data from the client to the server. When you pass the fsFile to your method, only the file info is sent and not the data. By contrast, when you do the insert directly on the client, it automatically chunks the file's data after insert, and then queues it to be sent chunk by chunk to the server. And then there is the matter of recombining all those chunks on the server and stuffing the data back into the fsFile. So doing client-side inserts actually saves you all of this complex work, and that's why we recommend it.

Have a look at HERE

So your method is not working because no data is sent to the server.

这篇关于流星文件上传不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆