【问题标题】:How to read file on client side in meteorjs?如何在meteorjs的客户端读取文件?
【发布时间】:2019-06-16 19:56:48
【问题描述】:

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

fs.readFileSync('Privatekey.pem');

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

【问题讨论】:

  • 你使用哪个前端? Blaze、React、Vue、Angular?
  • @Jankapunkt Blaze

标签: node.js meteor


【解决方案1】:

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

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

现在在此模板上,您使用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)
  },
})

【讨论】:

  • 谢谢@jankapunkt。我尝试过手动添加文件路径,因为我不需要 blaze,甚至 cz 它必须自己获取文件。常量文件='/home/zaid/Desktop/MeteorNew/MeteorProject/meteor/Public/rsa_1024_pub.pem';但它失败并出现错误“readAsText 函数的 Argumant1 不是对象”
猜你喜欢
  • 1970-01-01
  • 2014-08-22
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 2011-04-15
  • 2015-01-14
相关资源
最近更新 更多