Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

[Wordpress XML-RPC] Uploading an image to Wordpress

$
0
0

Hi everybody,

I'm using the XML-RPC.net framework to upload an image to Wordpress through Wordpress' metaWeblog.newMediaObject.

However, I'm stuck and keep running into the following error:

CookComputing.XmlRpc.XmlRpcTypeMismatchException: response
contains struct value where string expected (as type String)

As the error suggests, something is wrong with my struct. But I can't find it. Do you have an suggestion?

This is my code:

        [XmlRpcMissingMapping(MappingAction.Ignore)]
        public struct Image
        {
            public string name;
            public string type;
            public string bits;
            public bool overwrite;
            public int gallery;
            public int image_d;
        }

        // Upload image
        public void UploadImage(string imageURL)
        {
            this.Url = wordpressXmlrpcUrl;

            Image theImage     = new Image();
            theImage.name      = "myNewImage.png";
            theImage.bits      = Base64Image(imageURL);
            theImage.overwrite = false;
            theImage.type      = "image/png";            

            try
            {
                newImage("blogid", username, password, theImage, false);
                Console.WriteLine("Image successfully uploaded!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: ", ex.ToString());
                throw;
            }
        }

        [XmlRpcMethod("metaWeblog.newMediaObject")]
        public string newImage(string blogid, string username, string password, Image theImage, bool overwrite)
        {
            return (string)this.Invoke("newImage", new object[] { blogid, username, password, theImage, overwrite });
        }

PS:

  • Posting a blog post with just text to Wordpress works (so url, password, login are correct).
  • The method (Base64Image) that returns a Base64 string of the image works correctly. I've verified that by using an online Base64 decoder with the output of that method.

So, as I understand it, the problem lays not in what is send to Wordpress, buthow it's send.

Thanks in advance for any suggestion!



Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>