0

I am trying to upload my file to IPFS and save its hash to my Ethereum contract but neither ipfs.add nor ipfs.files.add is working for me... I have tried usin for loop i.e.

for await (const result of ipfs.add(this.state.buffer)) {
      console.log(result);
  }

and also this way:

ipfs.add(this.state.buffer, (error, ipfsHash) => 
    {
      console.log('Trying to add');
      if(error) {
        console.error(error)
        return
      }
    })

but both of them are not working.... I've tried to debug my code but the control doesn't go beyond ipfs.add line.... Any help will be highly recommended as I am not able to find any help on the internet and stuck here badly from past few hours..

0

Try replacing ipfs.add with ipfs.files.add.

| improve this answer | |
  • I have already tried using it but get this error: Uncaught (in promise) TypeError: ipfs__WEBPACK_IMPORTED_MODULE_4_.default.files.add is not a function at App.onSubmit – Usamafarooq459 Jul 20 at 12:19
  • Oh, my bad. I'm using ipfs-api version 18.1.1. If you cannot handle to make your code run you can try using ipfs-api, I'm using it for over 2 years now and I have hundreds of files uploaded to the IPFS via infura. Example: const ipfsAPI = require('ipfs-api'); const ipfs = ipfsAPI('ipfs.infura.io', '5001', {protocol: 'https'}); Now here you can access ipfs.files.add – Miroslav Nedelchev Jul 21 at 4:17
  • I have tried it as well... the error which I get on requiring ipfs-api is cannot find module... So I have installed ipfs-http-client...If try to upload it on infura the code doesn't show any error nor the control is passing from that piece of code... And if I try to upload on local daemon This Error shows up "Access to fetch at ''localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled." – Usamafarooq459 Jul 21 at 6:08
  • Of course you will get error for missing ipfs-api if you haven't installed it. Just open the console in the root of your project and fire npm install ipfs-api@18.1.1 --save-dev. Then you can try my example. – Miroslav Nedelchev Jul 21 at 6:11
  • I installed it before and installed it again using your npm command but still module not found error is showing up... I get few errors while installing ipfs-api and same was the case before... – Usamafarooq459 Jul 21 at 6:21

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.