【问题标题】:IPFS return file urlIPFS 返回文件地址
【发布时间】:2023-03-03 00:41:01
【问题描述】:

我正在尝试向 IPFS 执行文件上传功能,该功能返回文件所在位置的 url 字符串。但我不能让它退货。我尝试使用 var 和 window 来声明变量,但它总是返回 undefined。还在函数之外定义了 enlace,但它仍然总是声明 undefined。就像它不更新函数之外的值一样。作为打印 url 后的控制台日志。 (是的,我知道有一些垃圾代码,我一直在尝试调试问题)

    function upload() {
      const reader = new FileReader();
      reader.onloadend = function() {
        const ipfs = window.IpfsApi('localhost', 5001) // Connect to IPFS
        const buf = buffer.Buffer(reader.result) // Convert data into buffer
        ipfs.files.add(buf, (err, result) => { // Upload buffer to IPFS
          if(err) {
            console.error(err)
            return
          }
          window.enlace = `http://127.0.0.1:8080/ipfs/${result[0].hash}`
          console.log(`Url --> ${enlace}`)
          console.log(typeof enlace)
          //document.getElementById("url").innerHTML= url
          //document.getElementById("url").href= url
          //document.getElementById("output").src = url
        })
      }
      const photo = document.getElementById("photo");
      reader.readAsArrayBuffer(photo.files[0]); // Read Provided File
      return window.enlace;

    }

【问题讨论】:

    标签: javascript html blockchain ipfs


    【解决方案1】:

    我相信ipfs.add 是异步的,这意味着return window.enlacewindow.enlace 被设置在你的回调函数中之前执行。

    【讨论】:

      猜你喜欢
      • 2016-01-10
      • 2021-11-09
      • 2023-01-30
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 2022-01-24
      • 2017-01-26
      • 1970-01-01
      相关资源
      最近更新 更多