Meteor 之文件上传

1,需要添加两个文件上传的包

meteor add cfs:standard-packages
meteor add cfs:filesystem


2、在lib下创建file.js(也可以是其他的,主要是放在lib下)

//设置debug显示模式,这个很重要,可以前期帮你排错

FS.debug = true;
Images = new FS.Collection("images", {
    stores: [new FS.Store.FileSystem("images", {path: "~/uploads"})]
});
//定义http路径,
FS.HTTP.setBaseUrl('/Docs');


3、client创建模板和相关代码


<template name="docs">

<div class="form-group">
<label for="">身份证(照片)</label>
<input type="file" class="form-control fileupload" name="cardImage" multiple>

</div>

</template>


Template.docs.events({
'change .fileupload': function(event, template) {
console.log("change");
        FS.Utility.eachFile(event, function(file) {
            Images.insert(file, function (err, fileObj) {
//  console.log(err);
console.log("----",fileObj);

//可以使用http访问的url路径,
var url = FS.HTTP.uploadUrl;
//http全路径
console.log("doc",url+'/'+fileObj._id);


//上传成功后做了一个提示。
var success="<div class='alert alert-success' role='alert'>上传成功</div>";
                template.$(event.currentTarget).parent().append(success);

});
        });

    }
});

github官方路径

https://github.com/CollectionFS/Meteor-CollectionFS



评论

© H先生 | Powered by LOFTER