Upload Data to Meteor / Mongo DB

后端 未结 2 780
你的背包
你的背包 2021-02-04 19:03

I have a Meteor app and would like to upload data (from csv) to a meteor collection.

I have found:

  • solutions (e.g. Collectionfs) which deal with file uploa
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 19:50

    ChristianF's answer is spot on and I have accepted it as the correct answer. However, it provides even more than I need at this stage, so I am including here the code I have actually used - which is largely taken from Christian's answer and other elements I have found as a result:

    HTML UPLOAD BUTTON (I am not including drag and drop at this stage)

    
    

    JAVASCRIPT

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

    NB there is a jquery-csv library for Meteor here: https://github.com/donskifarrell/meteor-jquery-csv

提交回复
热议问题