有麻烦上传图像从iphone照片选取器到服务器使用ASIFormDataRequest

问题描述:

基本上我试图上传一个图像使用ASIFormDataRequest。这是我下面的代码有麻烦上传图像从iphone照片选取器到服务器使用ASIFormDataRequest

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]; 

基本上我的应用程序崩溃尔德给我以下错误:

[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' 

不知道为什么,但该方法使用setData似乎是在该实例。

几件事情。你得到的原因是因为第一行:

ASIFormDataRequest *request = [ASIHTTPRequest requestWithURL:urlImg]; 

正在创建一个ASIHTTPRequest类型的新对象。您需要:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:urlImg]; 

此外,您不需要设置RequestMethod,因为这是自动完成的。最后,如果服务确实需要上传一个XML文档。那么你将无法使用FormData。仅当您上传匹配的数据和HTML表单并使用多部分表单或网址编码数据编码时,格式数据才是。如果需要XML,您需要自己构建XML文档,然后从该文档发布数据。