[solved] Upload folder to IPFS

Hi, I am trying to upload an image file using the Moralis v2 api endpoint https://docs.moralis.io/web3-data-api/reference/upload-folder . I am having a problem for the param content. I am trying to convert my image file to base64 but the endpoint does not like the data. data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGcAAABQCAYAAAAEPmvsAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAPFSURBVHgB7ZzbTRtREIZnIVwkeKAEU0FacDogHRiBeU06ACqAvHGT4g5SQkgHlLAluADEZo7ZY4y9GO/u2Zn5rfkkZGQQF/3nfHuuQ+Q4juM4MmRkgOGw+JFl9JMA4L8zv73NvpEAX0iZwaDo8csFfxwQAByOSDAB9XC2t+kPgQTDXN7cZDkJoaq1s7PinF57DQL53V12SIJskBIzOoNgY0NOZ9PfSUqwzv4SDqI6i6iEU+qsRxgEnV2QAuLhIOmsKGisobOIeDhIOuNhs4rOIqJDaSSdca95vL/PrkkRsZ6DprPNTTomZcTCcZ3VRyQcJJ1xMCMenanqLNJ5OGCTzTz0GjJC5+Eg6YyfNSZ0Fuk0HDSd8ehsRIboLBzXWXs6C8d11p5OwmGdXZHrrDXJwzk9Lfr8ArHlTEZ1FkkaDj9nDnih8DeBYFVnkaTh8HPGR2cJSbbwuQ46K/8HVZ6fKR+NXntzknDCsBldZ0bOM+S7u2/7R0m0trODrTMrc7L5RtM6nOGwGPAPHRAGlTqzMCerajStwgktjn/oOYGwRGc90qWy0bQKB0lnHMwvFJ1FGoeDprO9vcUQrOos0igcNJ29vNDx9XU2nn3Pss4ijYbSQWfca3oEQNDZw0P2OPueFZ2FRsO7rvlHX6/dc1xnaahqNPPUCsd1lozKRjNPrXCQRmfMpWWdzTeaKlYOB01nVeebUXQWWSkcNJ1VnW+2ojMeNq+8OLxSOGg6m5/QWdFZ3UPxn4bjOktG7Y29peG4zpLR6I7P0nC2tiZ7ND3CYKFllptnF6RM0zs+H4ZT1gboEwYLLdPQeYbG5xQqw1mHy7RGzjO0urJYGU7QGfca2NoAVs4ztL2yuBCO6ywZrY9dvQunHJ2ZuJuyClUtk+dkFk6bJrmB/S4c9NoAVuZkqW5gT8NBrw1gaE6W7BTpdLONV0r/8Yvanfs67O/T0/x7RjYAkxaUMFFvrS1BZ9xr1AcBrLPDlGev1WrfpGIddRaBD8fIivlTF/Vx1IvhzRJ6AU+A+6t+P/eYr9qjs7KgxHfqAFPhlEP5HgHRZUEJM+GADeUndF0fx8QzB22hNSBRH0c9nLAWBrYyMUGiPo56OEhXFSNS9XFUwwG7qhgRu4GtWR0X6uZ1RPIGtmZ1XEidSd7A1qqOe0Sus0/Rqo57RWBoFJQQDwfs9OgErYISouGAnR6NqNXHEa2Oi3R6NKJZH0csHNdZfUTCcZ01Q6Q6ruusGZ2H4zprTqf7OScnxRG3wD5/mhMOY8vVCx3HcRzHcRwr/Ae9TckPt+mpQAAAAABJRU5ErkJggg==

Please help!

For reference, I also tried substring(23) on the above string to remove data:image/png;base64, but that also did not work. Here is the cloud function I am using on my self hosted server:

Parse.Cloud.define("uploadToIPFS", function ({params}: any){
  return Parse.Cloud.httpRequest({
    method: 'POST',
    url: "https://deep-index.moralis.io/api/v2/ipfs/uploadFolder",
    followRedirects: true,
    headers: {
      "content-type": "application/json",
      'X-API-Key': process.env.MORALIS_API_KEY,
      Accept: 'application/json',
    },
    body: JSON.stringify({
      "path": params.path,
      "content": params.base64File
    })
  }).then((httpResponse: any) => httpResponse, function(httpResponse: { status: string; }) {
    
  });
});

try only with this data and without the end lines from the end of the line

Hi this did not work either

did you remove the end of lines? that buffer has \n at the end of each line

Yup. I did this to the initial string I sent

fileInBase64.substring(22).replace(/(\r\n|\n|\r)/gm, "")

Here is my client side code if you need it:

const toBase64 = file => new Promise((resolve, reject) => {
                const reader = new FileReader();
                reader.readAsDataURL(file);
                reader.onload = () => resolve(reader.result);
                reader.onerror = error => reject(error);
            });
            const fileInBase64 = await toBase64(imageFile)
            console.log(fileInBase64)

            const params = {
                path: imageFile.name,
                base64File: fileInBase64.substring(23).replace(/(\r\n|\n|\r)/gm, ""),
            };
            console.log(params)

            const imageUri = await Moralis.Cloud.run("uploadToIPFS", params)
            .then((data) => {
                console.log(data.data)
                return data
            })
            .catch((error) => {
                console.log('Error: ', error);
            })

            console.log(imageUri)

Also, the server responds with an 500 error

try to add some debugging to see what you are sending as parameters

it looks like you added console.log, check that logging to see if it works as expected, you can also try directly in docs interface with those parameters

Hi the problem was the body needs to be an array.

    body: [{
      "path": params.path,
      "content": params.base64File
    }]
2 Likes