B A B A - 6 months ago 29
Ruby Question

Unable to upload images from Ruby to Wordpress using XMLRPC - damaged image

I am using a gem called rubypress to post content from ruby to wordpress using XMLRPC. Everything is working except for the image uploading part. I converted the image to base64 encoded format but after upoloading, all i get is a grey image and not the one that i had intended to upload(I am using smaller images around 100kb size to upload for testing). What I am i doing wrong?
Here's the code from rubypress github page(https://github.com/zachfeldman/rubypress) for uploading:

wp.uploadFile(:data => {
:name => FILENAME,
:type => MIME::Types.type_for(FILENAME).first.to_s,
:bits => XMLRPC::Base64.new(IO.read(FILENAME))


I was finally able to solve it by changing the Base64 encoding code as follows:

$wp.uploadFile(:data => {
    :name => File.basename(imgname),
    :type => MIME::Types.type_for(imgname).first.to_s,
    :bits => XMLRPC::Base64.new(File.open(FILENAME,"rb").read),
    :post_id =>postid


The file apparently needs to be "opened" first rather than be read using IO.read(FILENAME).