[SOLVED] File.ipfs() is undefined

const imageFile = new Moralis.File(uploadedFile.name, uploadedFile)
const result = await imageFile.saveIPFS()
console.log(result)
const imageURI = imageFile.ipfs()
console.log(imageURI)

I run a local moralis server. I upload a file/image, it works, there is also a result of saveIPFS(),

result:
{
“__type”: “File”,
“name”: “99e6cd71ab678186681cfa5e7654cf1e_appstore-1.png”,
“url”: “http://localhost:1337/server/files/001/99e6cd71ab678186681cfa5e7654cf1e_appstore-1.png
}

only that imageURI is undefined

any clues/reasons? thank you

You can upload separately to IPFS with uploadFolder.

It looks like saveIPFS() doesn’t work by default with self-hosted servers, if you check the result response, there is no data for _ipfs or _hash.

thank you. probably the official documentation should be updated to reflect this fact.

in response it gave the IPFS url but if i open the URL that doesn’t have any data
https://ipfs.moralis.io:2053/ipfs/QmNPkAxH4yw8NrGUiWW1r5U8n6HQYH81RRLJmNC3xub9Wc/moralis/Rectangle.png

maybe nothing was uploaded in that request for that ipfs folder?

No i uploaded the image with base64 format

Code:
let reader = new FileReader();

reader.readAsDataURL(image);

reader.onload = () => {

  setImageBase64(reader.result);

};

const options = {

  abi: [

    {

      path: `moralis/${image.name}`,

      content: imageBase64,

    },

  ],

};

const path = await Web3Api.storage.uploadFolder(options);

console.log(path);

there is empty data there for that url, maybe something didn’t work as expected when getting that base64 data

Is there any character limit for base64 data?

there is a limit in size, but you should get an error when you reach a limit

Issue is in limit if i upload the small image then IPFS generate the URL otherwise it gives an error so any solution for this ?

you can use other IPFS providers if you get to that size limit error

Okay thanks for the help

Hi, are you using parse server and cloud function to upload to IPFS? Could you please share the code in cloud function. I cant seem to get it right. Thank you

You can use uploadFolder to upload to IPFS. Try that exact example (JS or React) in your app that connects to your self-hosted server.

Tried the React code as is, just called that function on a button and it returns below error:

Also, what is the path and content here?
I have two requirements where
a. User uploads documents on UI and using the button they are stored on IPFS
b. In backend code generates some text content that needs to be put on IPFS and store the CID to be retrieved later.

My understanding is the path here is the image path (NFT context) can they be ignored in my both cases?
Can I achieve both using this code?

Thank you so much for all your help as always.

Can you post your code? There’s no address involved with uploadFolder and based on the error you didn’t pass in abi like the example.

path is the path name for what you’re uploading (see the example result in the docs page), content is what you’re uploading e.g. in base64 string format.

const uploadFolder = async () => {
    console.log(Moralis.User.current())
    const options = {
      abi: [
        {
          path: "moralis/logo.jpg",
          content:
            "iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAApgAAAKYB3X3",
        },
      ],
    };
    const path = await Web3Api.storage.uploadFolder(options);
    console.log(path);
    setValues({ ...values, message: path });
  };```

Can you try this - in your Parse Server’s src/cloud/generated/evmApi.ts file, change the uploadFolder cloud function code to:

Parse.Cloud.define('uploadFolder', async ({ params, user, ip }: any) => {
  try {
    await beforeApiRequest(user, ip, 'uploadFolder');

    delete params.address;
    delete params.chain;

    const result = await Moralis.EvmApi.ipfs.uploadFolder(params);
    return result?.raw;
  } catch (error) {
    throw new Error(getErrorMessage(error, 'uploadFolder'));
  }
});

And then rebuild and start your server again (npm run build > npm run start).

1 Like

Thanks, that worked. I will modify it to work with my usecases now. Thank you

1 Like