Ipfs.add() not working

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) => { //This isnt executed

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

  })

})

}

This is my code piece and i am getting an issue as i am unable to get results from ipfs. The output in the chrome console is mentioned below.

External Image

Hi @gupta-shrey, and welcome to the Infura community!

From the output, it looks like everything is posting to IPFS. Do you get a hash back that starts with Qm (I’m not sure, but it may be under that bottom dropdown arrow from your screenshot)? And how are you calling the get function to get your data back?

This tutorial may be of some use to you - the first half is uploading a file to IPFS, the second half is how to access that file. Let us know if you need any additional assistance!

As mentioned in the code what i want is that loading appears while file is being uploaded and main screen comes back when once file uploading is completed.
but here nothing happens. Either error or no error anything should have been console logged but none executed. I found that executing doesnt proceed the line

ipfs.add( this.state.buffer, (error, result) => {

Can you please help me with the same

I think I see what you may be doing here - it looks like you’re trying to add the this.state.buffer into IPFS, rather than the file, which is resulting in the section that isn’t being executed. You can check out a tutorial here that shows the proper way to set that up!

I’m facing the same issue with ipfs.add(), i’m unable to get the desired results like hash address of the file uploaded to IPFS.

Code.

onSubmit = (event) => {
event.preventDefault()
console.log(‘Submitting the file’)
ipfs.add(this.state.buffer, (error, result) => {
console.log(‘Ipfs result’, result)
if(error){
console.error(error)
return
}
})

Hey Updating the baseUri, syncing and uploading the metadata and file to ipfs at doesn’t run at all. I keep getting
const fetch = require(‘node-fetch’);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module C: … index.js from C:(file location)/uploadFiles.js not supported
Instead change the require of index.js in C:(file location)uploadFiles.js to a dynamic import() which is available in all CommonJS modules.
at Object. (file location) {
Code: ‘ERR_REQUIRE_ESM’}
Please how do I resolve this cus it’s a barrier

Hello everyone, I am new here. I am facing some issue with IPFS. the following code i am using for image upload to ipfs.infura.io gateway in my form. it was working fine but from few days it’s giving me this error. need your kind help!

Error: /api/v0/object has been removed, use /api/v0/dag instead

Hi :wave: @younas and welcome to the Infura community!

Could you share with us a code snippet you used for image upload?

Are you using /api/v0/object by change? Please note, that method has been deprecated and you should use /api/v0/dag instead.

hello, I got the same problem. When I run the program, it shows "https://ipfs.infura.io:5001/api/v0/add?stream-channels=true&progress=false CORS request did not succeed ". I tried to use the URL that I run successfully before, but now I can’t get in and show this message.

Is there any way to fix it to make it run successfully?

Hey @Cal please refer to the following.
https://infura.discoursehosting.net/t/ipfs-authentication-error-403-forbidden/5672/50
The change was reverted that was affecting client-agents, but you still should use Dedicated getaway as of now, as the Public getaway has been deprecated.
https://docs.infura.io/infura/networks/ipfs/how-to/access-ipfs-content/public-gateway

hey can you help me? yesterday my project was working successfully and was able to upload files and get that hash. today it gives this message in console
POST https://ipfs.infura.io:5001/api/v0/add?stream-channels=true&progress=false 401 (Unauthorized)

This is what i do in my code:-

** Setting up ipfs client: **

import { create } from 'ipfs-http-client'
const ipfs = create({ host: 'ipfs.infura.io', port: 5001, protocol: 'https', apiPath: '/api/v0' });

** Uploading my file : **

const uploadFile = description => {

    // console.log("ipfs client : ", ipfs)

    // console.log("submitting the file to IPFS with description: ", description)

    // console.log("buffer is : ", buffer);

    //Add file to the IPFS

    //from infura documentation

    ipfs.add(buffer)

      .then(

        result => {

          console.log("result is ", result)

          //Set state to loading

          setLoading(true);

          //Assign value for the file without extension

          if (type == null) {

            setType('none')

          }

          console.log("our smart contract is ", ddBox)

          console.log("our method is ",ddBox.functions.uploadFile)

         

        const callMethod=async ()=>{

          try {

           

           const tx= await ddBox.functions.uploadFile(result.size, result.path, type, name, description);

           console.log("transaction is : ",tx)

           console.log(tx.hash)

           setLoading(false);

            setType(null);

            setName(null);

            window.location.reload()

          } catch (error) {

            window.alert('Error')

            setLoading(false)

          }

        }

        callMethod();

        }

        )

      .catch(error => console.error(error))

  }

@lovekosmas It worked! Thank you!

1 Like

@lovekosmas @mike can someone please help. i am trying from 3 days to resolve this

@Abhinav_Agarwal, what is in your “buffer”?
Can you make sure you are including Authentication when uploading files?

Please check out this example:
https://infura.discoursehosting.net/t/how-to-add-files-using-ipfs-http-client/5180

I am also getting the same issue anyone please help … Facing since 3 weeks (same issue) … :frowning_face:

The issue :
fetch.browser.js?c17b:107 POST https://ipfs.infura.io:5001/api/v0/add?stream-channels=true&progress=false 401 (Unauthorized)

The github link for the file :

https://github.com/Vikash-8090-Yadav/Sal-DApp/blob/main/Sal-DAp/Component/Form/component/Addemp.js

@mike @lovekosmas someone please help…Getting this error since 3 weeks. :disappointed:

Hi @Vikash_Kumar_yadav You’ll need to pass authentication to your IpfsApi, here’s an example How to add data using ipfs-http-client

@Flaveeu while i am making project in the ipfs at that time it is asking for my card details . Is this true ?