在react js中上传文件到ipfs时出错

发布于 2025-01-13 23:05:05 字数 1969 浏览 1 评论 0原文

我尝试在 React js UI 中将图像上传到 ipfs。当使用添加按钮添加图像时,它可以工作,但是当单击提交按钮时,会出现下一个错误 “未捕获(承诺中)错误:意外输入:null normalise-candidate-single.js:16”,然后将 ipfs 的返回哈希存储在 hypeledger 结构中

谁可以帮助我?

我的App.js文件如下
**

import React, { Component } from 'react';
import './App.css';
import { create } from 'ipfs-http-client'

const ipfs = create('http://localhost:5001') 


class App extends Component {
   constructor(props) {
    super(props)
    this.state = {
      buffer: null
    }
  }
 
  
 captureFile = (event) => {
   event.preventDefault()
   //process file for IPFS...
   const file = event.target.files[0]
   const reader = new window.FileReader()
   reader.readAsArrayBuffer(file)
   reader.onloaded = () => {
     this.setState(('buffer', Buffer(reader.result)))
   }

 }
 onSubmit = (event) => {
   event.preventDefault()
   console.log("submiting the form....")
   ipfs.add(this.state.buffer, (error, result) => {
    console.log('Ipfs result', result)
    if(error) {
      console.error(error)
      return
    }
 })
}
 

  
      
      render() {
        return (
                                    
         
      <div>
        
        <div className="container-fluid mt-5">
          <div className="row">
            <main role="main" className="col-lg-12 d-flex text-center">
              <div className="content mr-auto ml-auto">
                 
                <h2>Change Logo</h2>
                <form onSubmit={this.onSubmit} >
                  <input type='file' onChange={this.captureFile} />
                  <input type='submit'/>
                </form>
              </div>
            </main>
          </div>
        </div>
      </div>
    );
  }
}
              
 export default App;
 

**

I trying to upload image to ipfs in react js UI. when add image using add button its work but when click on submit button get the next error
"Uncaught (in promise) Error: Unexpected input: null normalise-candidate-single.js:16 " and then to store return hash from ipfs in hypeledger fabric

who can help me?

My App.js file as follow
**

import React, { Component } from 'react';
import './App.css';
import { create } from 'ipfs-http-client'

const ipfs = create('http://localhost:5001') 


class App extends Component {
   constructor(props) {
    super(props)
    this.state = {
      buffer: null
    }
  }
 
  
 captureFile = (event) => {
   event.preventDefault()
   //process file for IPFS...
   const file = event.target.files[0]
   const reader = new window.FileReader()
   reader.readAsArrayBuffer(file)
   reader.onloaded = () => {
     this.setState(('buffer', Buffer(reader.result)))
   }

 }
 onSubmit = (event) => {
   event.preventDefault()
   console.log("submiting the form....")
   ipfs.add(this.state.buffer, (error, result) => {
    console.log('Ipfs result', result)
    if(error) {
      console.error(error)
      return
    }
 })
}
 

  
      
      render() {
        return (
                                    
         
      <div>
        
        <div className="container-fluid mt-5">
          <div className="row">
            <main role="main" className="col-lg-12 d-flex text-center">
              <div className="content mr-auto ml-auto">
                 
                <h2>Change Logo</h2>
                <form onSubmit={this.onSubmit} >
                  <input type='file' onChange={this.captureFile} />
                  <input type='submit'/>
                </form>
              </div>
            </main>
          </div>
        </div>
      </div>
    );
  }
}
              
 export default App;
 

**

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文