0

I am trying to upload an image to ipfs in my svelte app. This is the code where I try to upload the image:

const uploadFile = async (file) => {

    const ipfs = await IPFS.create();

    const result = await ipfs.add(file);
    console.log(result)
}

file is in ArrayBuffer format.

However, it returns the error saying:

(8) index.js:54 Uncaught (in promise) TypeError: callback is not a function
    at Function../node_modules/peer-info/src/index.js.PeerInfo.create (index.js:54)
    at GossipSub._onIncomingStream (index.js:174)
    at Upgrader._onStream (upgrader.js:313)
    at Mplex.onStream (upgrader.js:235)
halfer
  • 19,824
  • 17
  • 99
  • 186
jasonph
  • 134
  • 5

2 Answers2

0

Thank you, Mionutm!!

That was helpful. The problem was the filewas not in the right format. I thought ipfs expects an ArrayBufferso I read the file into ArrayBuffer format.

However, we could have added the uploaded file to ipfs just by accessing its property as follow:

const file = e.srcElement.files[0];
for await (const result of ipfs.add(file)) {
    console.log(result);
}

That solved the add <suspended> and the invalid content errors.

Nonetheless, the TypeError: callback is not a function is still unsolved, which i think it is the error produce by the node_modules (not sure).

jasonph
  • 134
  • 5
-2

You can try adding the file by using ipfs.add in a for loop:

for await (const result of ipfs.add(file)) {
    console.log(result);
}

More info here: https://github.com/ipfs/js-ipfs/blob/master/docs/core-api/FILES.md#add

Mionutm
  • 58
  • 4