2011-03-30 11 views
3

이미지 데이터를 서버에 보내려고합니다. 그 요청에 대한 HTTP 본문에 데이터를 첨부하여 보냈습니다. 다음 코드를 사용하고 있습니다.서버에 이미지 데이터 (NSData) 보내기

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:@"http://developers.our-works.com/forms/TestReceiver.aspx"]]; 
[request setHTTPMethod:@"POST"]; 

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

// now lets create the body of the post 
NSMutableData *body = [NSMutableData data]; 

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",tempFileName] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:imgdata]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

//data into the string..... verifying............ 
NSString *strTest = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding]; 
NSLog(@"%@",strTest); 

// setting the body of the post to the request 
[request setHTTPBody:body]; 

// now lets make the connection to the web 
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

이제 두 개의 서로 다른 이미지의 NSData를 보내야합니다. 그래서, 그냥 물어보고 싶은데, 그 두 개의 서로 다른 이미지의 NSData를 위와 같은 몸체로 보낼 수 있습니까? 또는 다른 이미지의 데이터에 대해 동일한 코드를 작성해야합니다.

제발 도와주세요. 고마워. 서버가 함께 그것은 service.you는 다음 링크에서 아이디어를 취할 수있는 웹에 의존 두 이미지를 필요로하는 경우

답변

2

약간 제안 : 쉽게 사용할 수 Ben Copsey의 ASIHTTPRequest은 POST 데이터를 서버에 전달합니다.

POST에 두 번 코딩 할 필요가 없습니다. 이미지를 두 번 다른 키로 두 번 삽입하면됩니다. 아래 예제 참조 (ASIHTTPRequest 사용)

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
[request setPostValue:@"Ben" forKey:@"first_name"]; 
[request setPostValue:@"Copsey" forKey:@"last_name"]; 
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"]; 
[request setFile:@"/Users/ben/Desktop/ben2.jpg" forKey:@"photo2"]; 
[request startSynchronous];