2012-01-12 82 views
0

我一直避免使用ASIFormDataRequest,因爲它不太適合我的需求,並且有太多的代碼來滿足我的簡單需求。iOS上傳圖像文件NSData並獲取POST PHP變量

SO:

我發現了幾個其他的代碼塊,但是其中沒有一個成功地傳送和vraiables或上方作爲圖像不顯示並且響應doesn'y顯示VARS的圖像數據。

下面是代碼:

NSData * imageData = myImageData; 

NSString *urlString = [[self getDBCGIFolder] stringByAppendingString:DATABASE_PATH_UPLOAD_PORTRAIT]; 

NSString *values = @"&accountID:"; 
values = [values stringByAppendingString:myAccountID]; 
values = [values stringByAppendingString:@"&shit:"]; 
values = [values stringByAppendingString:@"SHITE"]; 
NSData *valueData = [values dataUsingEncoding:NSUTF8StringEncoding]; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:urlString]]; 
[request setHTTPMethod:@"POST"]; 

NSLog(urlString); 

NSMutableData *postBody = [NSMutableData data]; 
[postBody appendData:[NSData dataWithData:imageData]]; 
[postBody appendData:[NSData dataWithData:valueData]]; 


[request setHTTPBody:postBody]; 

// now lets make the connection to the web 
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

NSLog(@"--%@", returnString); 

此代碼讓我最遠的,在XML堅實的回覆字符串,讓我檢查的PHP腳本到達的數據,但沒有數據似乎那裏。

有人嗎?

+0

'沒有數據似乎到達那裏'你的意思是在請求正文0字節或腳本無法解析它以你期望的方式嗎? – 2012-01-12 22:09:46

回答

0

它看起來像要提交多部分表單數據,但在該代碼中,您將追加字符串和圖像數據而不將它們編碼爲單獨的值。 ASIHTTPRequest和AFNetworking都提供了這種功能,所以雖然看起來更容易推出自己的產品,但更好的方式是讓其中一個更深一層。