2010-06-28 61 views
0

基本上我試圖上傳一個圖像使用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似乎是在該實例。

回答

2

幾件事情。你得到的原因是因爲第一行:

ASIFormDataRequest *request = [ASIHTTPRequest requestWithURL:urlImg]; 

正在創建一個ASIHTTPRequest類型的新對象。您需要:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:urlImg]; 

此外,您不需要設置RequestMethod,因爲這是自動完成的。最後,如果服務確實需要上傳一個XML文檔。那麼你將無法使用FormData。僅當您上傳匹配的數據和HTML表單並使用多部分表單或網址編碼數據編碼時,格式數據纔是。如果需要XML,您需要自己構建XML文檔,然後從該文檔發佈數據。