如何在 meteorjs 中读取客户端文件?

How to read file on client side in meteorjs?

我是流星和 nodejs 的新手。我正在 meteor 中开发一个应用程序,我需要从本地机器上的目录读取私钥 (Privatekey.pem) 文件。由于此私钥将用于购买客户端来签署一些数据。我需要客户端读取该私钥并签署数据。我已经试过了

fs.readFileSync('Privatekey.pem');

但是它失败了,因为你不能在客户端 fs.readFileSync is not a function Meteor, React 上使用 "fs" 。所以请指导我如何在客户端读取文件。

您可以轻松地将 FileReader API 与文件类型的输入结合使用:

<template name="myReader">
  <input type="file" id="my-file-input" />
</template>

现在在这个模板上,您 "listen" 使用 Blaze event maps:

my-file-inputchange 事件
Template.myReader.events({
  'change #my-file-input' (event, templateInstance) {
    const file = event.currentTarget.files[ 0 ]
    const reader = new FileReader()
    reader.onload = function () {
      const text = reader.result
      // do Meteor.call() or
      // add it to a reactive variable
      // on your templateInstance
    }
    reader.readAsText(file)
  },
})