2012-02-04 21 views
1

我想從我的iPhone應用程序中使用POST方法將用戶圖片和用戶的其他信息發送到服務器。forHTTPHeaderField:@「content-type」用於圖像和數據

我的內容類型如下

NSString *boundary = [NSString stringWithString:@"--"]; 
     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
     [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

但只張貼圖片。我的其他數據不在服務器上發佈。

如果我使用下面的內容類型,然後

[ request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 

則只能發送其他數據。不發送圖像。

請向我建議我可以發送圖像和其他信息的方式。

回答

-2

使用ASIHTTPRequest其簡單易用的:)

+0

爲什麼?這不再是積極的發展http://allseeing-i.com/[request_release]。當有人向某人詢問實現細節時,只需鏈接到某個框架並不是一個好的答案。 – Abizern 2012-02-04 10:38:47

+0

它不再積極的發展,但它仍然工作順利,我給他一個替代方案,因爲即使在45分鐘後沒有人回答他的問題 – 2012-02-04 10:57:24

+1

但引用他的框架仍然不是他的問題的答案。 – Abizern 2012-02-04 11:25:03

0

編碼圖像的Base64使用這樣

[Base64Coder encodeData:UIImagePNGRepresentation(image)]; 

您可以從this link下載的Base64類。

編輯:添加的代碼示例

NSString *reqStr = [NSString stringWithFormat:@"iPhone_request=<game><name>%@</name><image>%@</image></game>",txtGameName.text, [Base64Coder encodeData:UIImagePNGRepresentation(imgGame.image)]]; 

// Replace occurrences of + with %2B 
reqStr = [[reqStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"]; 
reqData = [reqStr dataUsingEncoding:NSUTF8StringEncoding]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]]; 
[request setHTTPMethod:@"POST"]; 
[request setHTTPBody:reqData]; 
相關問題