2

I'm running the script below and using an ipfs node to upload and get a file using its hash, but the ipfs cat function only returns the path of the file from the hash, not the content.

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

Does anyone have experience with js-ipfs and can help me out?

1 Answer 1

2

I had a similar problem with ipfs. if you have hash already you can get the content this way.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.