我有一个 Meteor 应用程序,想将数据(从 csv)上传到流星集合。
我已经发现:
- 解决方案(例如集合fs https://github.com/CollectionFS/Meteor-CollectionFS) 处理文件上传
- 方法用于直接上传 http://docs.mongodb.org/manual/reference/program/mongoimport/#bin.mongoimporty 从 shell 到底层 mongo db
- 参考流星路由器 - 但我正在使用优秀的铁路由器 https://github.com/EventedMind/iron-router,它似乎没有提供此功能
我的要求是应用程序用户能够从应用程序内将 csv 数据上传到应用程序。我不需要将 csv 文件存储在应用程序文件结构中的任何位置,我只需要read将 csv 数据添加到集合中。
我可能无法弄清楚如何做到这一点,因为我的职权范围(“将数据上传到流星”)不明确或不正确。或者说我是个白痴。
ChristianF 的回答 https://stackoverflow.com/a/21053576/2581585是正确的,我已接受它作为正确答案。然而,它提供的功能甚至比我现阶段需要的还要多,所以我在这里包含了我实际使用的代码 - 这很大程度上取自 Christian 的答案和我由此发现的其他元素:
HTML 上传按钮(现阶段我不包括拖放)
<template name="upload">
<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>
</template>
脚本语言
Template.upload.events({
"change #files": function (e) {
var files = e.target.files || e.dataTransfer.files;
for (var i = 0, file; file = files[i]; i++) {
if (file.type.indexOf("text") == 0) {
var reader = new FileReader();
reader.onloadend = function (e) {
var text = e.target.result;
console.log(text)
var all = $.csv.toObjects(text);
console.log(all)
_.each(all, function (entry) {
Members.insert(entry);
});
}
reader.readAsText(file);
}
}
}
})
注意,这里有一个 Meteor 的 jquery-csv 库:https://github.com/donskifarrell/meteor-jquery-csv https://github.com/donskifarrell/meteor-jquery-csv
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)