Search code examples
c#wordpressbase64xml-rpcnextgen-gallery

Failing to upload Image in base64 bits via XML-RPC [Wordpress/NGG]


I have literally tried every possible way on this but apparently I just can't figure out the right one.

I am trying to upload images to the next-gen gallery that is installed on a wordpress blog. Everything about xml-rpc is working since I am able to do all other stuff with it.

The problem is that server returns an error saying the image is not a valid one. Which is quite obvious that the problem is about base64. However, for test purposes i copied and checked the base64 string and realized it is just right, it converts right to the image using an external base64 to image converter.

This is the line that submits the query.

    result = categories.newImage(1, "admin", "password", newImage);

The struct for newImage is;

    public struct imageI
    {
        public string name;
        public string type;
        public string bits;
        public bool overwrite;
        public int gallery;
        public int image_id;

    }

And this is how a new one is initialized, along with converting an image into base64

        //Creating the image base64 string
        string filename = "asd.jpg";
        Image image1 = Image.FromFile(filename);
        string base64 = ImageToBase64(image1, image1.RawFormat);
        //for test purposes i copied and checked the base64 and it is just right, it converts right to the image using an external base64 to image converter.
        Clipboard.SetText(base64);

        //Creating a newImage 
        imageI newImage = default(imageI);
        newImage.name = "newImage";
        newImage.bits = base64;
        newImage.gallery= 86;

And finally my method "ImageToBase64(Image, ImageFomat)";

    public string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            // Convert Image to byte[]
            image.Save(ms, format);
            byte[] imageBytes = ms.ToArray();

            // Convert byte[] to Base64 String
            string base64String = Convert.ToBase64String(imageBytes);
            return base64String;

        }
    }

Solution

  • In case anybody bumps on this, I figured it out, on the line; newImage.name = "newImage"; the file name should be the same with the one you are uploading, or at least it should have the same extension so that the function in xml-rpc file can resolve the extension and check if it's okay to upload or not.