JoeBlogs
JoeBlogs copied to clipboard
How to upload an image using joeblogs on wordpress blog?
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?
You need to upload a byte[] You're uploading a string of "C:\Users\Thomas janes\Pictures\qw.JPG"
You mean that i should convert the filepath type to byte[] rather than String or What? Please give me an example?
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
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?
Use fiddler Find out what is actually being sent to the server
Ok Sir, i look into it!
Content-Length: 33547 Content-Type: text/xml
And fault message is being sent as response from the server!
What else should i try? I mean could you provide your snippet to upload an image so that i could try!
No, look at the actual content of the message being sent. The body On 9 Jul 2013 12:17, "Shahid Sultan Minhas" [email protected] 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 .
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!
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 [email protected] 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 .
Text view of the packet!
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?
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 [email protected] 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 .
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. :)
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
@Shahid20 - did @CMHouse suggestion work for you?