views:

102

answers:

2

Basically I am trying to upload an image using ASIFormDataRequest. This is my following code

ASIFormDataRequest *request = [ASIHTTPRequest requestWithURL:urlImg];
[request setRequestMethod:@"POST"];
[request addRequestHeader:@"Content-Type" value:@"application/xml"];
[request setDelegate:self];

[request setTimeOutSeconds:500];  
NSData *imageData = UIImagePNGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"]);
[request setData:imageData forKey:@"media"];
[request startAsynchronous];

Basically my app wold crash and give me the following error:

[ASIHTTPRequest setData:forKey:]: unrecognized selector sent to instance 0x8880db0
2010-06-28 12:33:49.803 vdq[7658:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[ASIHTTPRequest setData:forKey:]: unrecognized selector sent to instance 0x8880db0'

Not sure why, but the method setData seems to be in that instance.

+1  A: 

Couple of things. The reason you are getting that is because of the first line:

ASIFormDataRequest *request = [ASIHTTPRequest requestWithURL:urlImg];

is creating a new object of the type ASIHTTPRequest. You need to do:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:urlImg];

Also, you don't need to set the RequestMethod, as that is done automatically. Finally, if the service really needs an XML document uploaded. Then you won't be able to use the FormData. The format data is only if you are uploading data that matches and HTML form and is encoded with multipart form or url encoded data. If it requires, XML, you will need to construct the XML document yourself and then post the data from that document.

christophercotton
A: 

Also have some questions.

Question 1:

in above codes,

[request setData:imageData forKey:@"media"];

seems like there are a lot keys defined, for newbies so hard to understand and use them, i.e, why need to use "media" as key in this case.

So for usual case, how to completely understand and know those keys ? Thanks,

Question 2:

Why do not need set method type, "Get" is different with "Post" , how can they know it automatically ?

Forrest