"Could not decode base64" error when I try to upload

I am having trouble uploading to my cloud via base64 encoded image.

Here is the encoded string: https://ctxt.io/2/AACw8dghFQ


My code is:

 const base64Url = changedDesktopPortrait.valueOf() as string;

const base64Data = base64Url.replace(/(\r\n|\n|\r)/gm, "");

const desktopRes = cloudinary.uploader

        .upload_large(base64Data, {

          folder: uploadFolder,

          public_id: "desktopPortrait",

          overwrite: true,

          resource_type: "image",

        })

        .then((res) => {

          return res;

        });

Answers

  • epasos
    epasos Member, Cloudinary Staff Posts: 21

    Hi,

    To upload an image using its base64 format, the actual Base64 data can be used (i.e. without encoding it) - for example:

    const base64Image = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
    const uploadFolder = 'some_folder';
    
    result = cloudinary.uploader.upload_large(base64Image, {
     folder: uploadFolder,
     public_id: "desktopPortrait",
     overwrite: true,
     resource_type: "image",
    })
     .then((res) => {
      console.log(res);
      return res;
     });
    

    For Upload API documentation, see: https://cloudinary.com/documentation/image_upload_api_reference#upload_required_parameters

    Hope this helps. Let me know if further clarification is needed.