I am working on a dapp and wish to upload a file on ipfs and retrieve its hash to be used further. The concerned code is as follows
const ipfsClient = require(‘ipfs-http-client’)
const ipfs = ipfsClient({ host: ‘ipfs.infura.io’, port: 5001, protocol: ‘https’ })
captureFile = event => {
event.preventDefault()
const file = event.target.files[0]
const reader = new window.FileReader()
reader.readAsArrayBuffer(file)
reader.onloadend = () => {
this.setState({ buffer: Buffer(reader.result) })
console.log('buffer', this.state.buffer)
}
}
uploadAsset = (cost) => {
console.log("Submitting file to ipfs...")
ipfs.add( this.state.buffer, (error, result) => { //Execution stops here
console.log('Ipfs result', result)
if(error) {
console.log("Error")
console.error(error)
return
}
console.log("no error")
this.setState({ loading: true })
this.state.dstock.methods.uploadAsset(result[0].hash, cost).send({ from: this.state.account }).on('transactionHash', (hash) => {
this.setState({ loading: false })
})
})
The output is as given below. The code isn't returning the result.