首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从js-ipfs中的散列中获取文件

从js-ipfs中的散列中获取文件
EN

Stack Overflow用户
提问于 2018-05-10 07:55:00
回答 1查看 1.6K关注 0票数 0

我正在运行下面的脚本,并使用ipfs节点通过其散列来上传和获取文件,但是ipfs cat函数只从散列返回文件的路径,而不返回内容。

代码语言:javascript
复制
const node = new Ipfs()

node.once('ready', () => console.log('IPFS node is ready'))

$("#saveIt").click(function(){
  var toStore = document.getElementById('fileInput').value

  node.files.add(new node.types.Buffer.from(toStore), (err, res) => {
    if (err || !res) {
      return console.error('ipfs add error', err, res)
    }

    res.forEach((file) => {
      if (file && file.hash) {
        var newVar = file.hash
        var newVar1 = newVar.slice(0, 23)
        var leng = newVar.length
        var newVar2 = newVar.slice(24, leng)
        console.log(newVar1 + ' ' + newVar2)
        mediachain.setUserFile($("#passwordSetter").val(), newVar1, newVar2)
        node.files.cat(file.hash, (err, data) => {
          if (err) {
            return console.error('ipfs cat error', err)
          }
          document.getElementById('fileDisplayArea').innerText = data
        })
      } else {
        console.error("Error: invalid file")
      }
    })
  })
})

有没有人有使用js-ipfs的经验,可以帮我?

EN

回答 1

Stack Overflow用户

发布于 2018-05-25 19:04:04

我在ipfs上也遇到过类似的问题。如果你已经有了哈希,你可以通过这种方式获取内容。

代码语言:javascript
复制
const validCID = 'QmQFPQ5f94byxs7zvHMLJcx5WzThRhN4MfAF4ZisSXofKC'

ipfs.files.get(validCID, function (err, files) {
files.forEach((file) => {
console.log(file.path)
console.log("File content >> ",file.content.toString('utf8'))
})
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50263605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档