alexjamesbrown / JoeBlogs

Wordpress / Metaweblog API Wrapper
53 stars 40 forks source link

How to upload an image using joeblogs on wordpress blog? #28

Open Shahid20 opened 11 years ago

Shahid20 commented 11 years ago

I am new to joeblogs wordpress remote publishing liberary and trying to upload an image with some post to my wordpress blog! Any code snippet to upload an image?

I have tried that string:

wrapper.UploadFile(@"C:\Users\Thomas janes\Pictures\qw.JPG","ERD Diagram",true,"image/jpeg"); but it returned me an exception saying that Server returned a fault exception: [500] Could not write file wpid-ERD-Diagram (Invalid file type). Whta does this mean and why is it occurring?

alexjamesbrown commented 11 years ago

You need to upload a byte[] You're uploading a string of "C:\Users\Thomas janes\Pictures\qw.JPG"

Shahid20 commented 11 years ago

You mean that i should convert the filepath type to byte[] rather than String or What? Please give me an example?

alexjamesbrown commented 11 years ago

Yes, But it seems that it should be.

Can you use Fiddler and find out what is going over the wire?

I suspect it's an issue like this: http://wordpress.org/support/topic/error-could-not-write-file-wpid-m-invalid-file-type

In which case it's a bug in the library

Shahid20 commented 11 years ago

Sorry Sir, i read the post but i didn't get what actually is to be done! I reckon there is a need to amend sort of data type in your liberary or what? Please let me know what other solution i can try?

alexjamesbrown commented 11 years ago

Use fiddler Find out what is actually being sent to the server

Shahid20 commented 11 years ago

Ok Sir, i look into it!

Shahid20 commented 11 years ago

Content-Length: 33547 Content-Type: text/xml

And fault message is being sent as response from the server!

Shahid20 commented 11 years ago

What else should i try? I mean could you provide your snippet to upload an image so that i could try!

alexjamesbrown commented 11 years ago

No, look at the actual content of the message being sent. The body On 9 Jul 2013 12:17, "Shahid Sultan Minhas" notifications@github.com wrote:

Content-Length: 33547 Content-Type: text/xml

And fault message is being sent as response from the server!

— Reply to this email directly or view it on GitHubhttps://github.com/alexjamesbrown/JoeBlogs/issues/28#issuecomment-20667451 .

Shahid20 commented 11 years ago

Yes i checked the body in xml view,its actually sending the image with sort of tags and there i could see a tag like base64 and sort of numerical code there! I am new to fiddler so probably i can't be telling you explicitly what you want to know about! But please if you could show me a method of uploading an image, would then certainly be appreciated! Thanks Sir!

alexjamesbrown commented 11 years ago

can you copy paste exactly what is in the body onto here, i'll take a look

On 9 July 2013 14:46, Shahid Sultan Minhas notifications@github.com wrote:

Yes i checked the body in xml view,its actually sending the image with sort of tags and there i could see a tag like base64 and sort of numerical code there! I am new to fiddler so probably i can't be telling you explicitly what you want to know about! But please if you could show me a method of uploading an image, would then certainly be appreciated! Thanks Sir!

— Reply to this email directly or view it on GitHubhttps://github.com/alexjamesbrown/JoeBlogs/issues/28#issuecomment-20674614 .

Shahid20 commented 11 years ago

Text view of the packet! <?xml version="1.0" encoding="UTF-8"?>

faultCode 500 faultString Could not write file wpid-ERD-Diagram (Invalid file type)

Or tell me how can i get to content of packet if you have any aquaintance with fiddler? I mean when i click on a packet, there you can see many options on the right of window where acc. to me, i am clicking on inspector tab and there you can see lot many other options like which view you want to see like xml, json,text view etc!

By far, i have converted the String to byte via this code of mine as: static byte[] GetBytes(string str) { byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); return bytes; } but when i atart adding parameters to method as: String pic = @"C:\Users\M.Shahid.Sultan\Pictures\qw.JPG"; // path of image byte[] newpic = GetBytes(pic); // method to convert String to byte array.

wrapper.UploadFile(newpic, "ERD Diagram", true, "image/jpg"); // method referring your liberary to upload an image. So it gives me error that first of the parameter in uploadFile method is not byte array(as you said it must be rather than String), so what should i need to change then in your liberary or what else?

Shahid20 commented 11 years ago

wp1 wp2 wp3

alexjamesbrown commented 11 years ago

Looks ok to me... I don't know what's wrong, and unfortunately I have no time immediately to have a look

Will have to leave the issue open for when I get time. It's open source, so feel free to take a look yoursel

On 9 July 2013 15:30, Shahid Sultan Minhas notifications@github.com wrote:

[image: wp1]https://f.cloud.github.com/assets/4085893/768623/e34b013c-e8a3-11e2-8175-51909e900541.JPG [image: wp2]https://f.cloud.github.com/assets/4085893/768626/eeb94326-e8a3-11e2-8621-91a622e60b50.JPG [image: wp3]https://f.cloud.github.com/assets/4085893/768632/03747fce-e8a4-11e2-842f-7d49a83e53b9.JPG

— Reply to this email directly or view it on GitHubhttps://github.com/alexjamesbrown/JoeBlogs/issues/28#issuecomment-20677698 .

caffedrine commented 10 years ago

I think I know what the problem is. You have to convert IMAGE to byte arry, not image path. You have there a simple snnipet:

        var wp = new WordPressWrapper(link + "/xmlrpc.php", username, password);
        byte[] imageData = System.IO.File.ReadAllBytes("image.png");
        var image = wp.NewMediaObject(new MediaObject { Bits = imageData, Name = "Plane.jpg", Type = "image/jpeg" });

This will work for you. :)

CMHouse commented 9 years ago

Shahid, Your code should be like this: wrapper.UploadFile(@"C:\Users\Thomas janes\Pictures\qw.JPG","ERD-Diagram.jpg",true,"image/jpeg");

You were missing the file extension for the name of the file. It's asking for uploaded filename, not image title.

Hope this helps

alexjamesbrown commented 9 years ago

@Shahid20 - did @CMHouse suggestion work for you?