저는 지난 2 ~ 3 일 동안 문제가 생겼습니다. 나는 여러 부분으로 구성된 서버에 요청을 보내야합니다. 이미지와 json, 다중 요청/양식 데이터를 사용하여 요청을 보냈지 만 어떤 이유로 서버가 요청을받을 수 없었으므로 아무도이 문제를 해결할 수 없습니다.다중 이미지/혼합을 사용하여 ios의 json과 함께 업로드 이미지
사용중인 코드는 다음과 같습니다.
NSData *imageData = UIImagePNGRepresentation(img);
NSMutableURLRequest *theRequest =[NSMutableURLRequest requestWithURL:theURL];
NSString *boundary = [NSString stringWithFormat:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[theRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPMethod:@"POST"];
NSMutableString *theBody = [[NSMutableString alloc]init];
[theBody appendString:[NSString stringWithFormat:@"\r\n--%@\r\n", boundary]];
[theBody appendString:[NSString stringWithFormat:@"Content-Type: application/json\r\n\r\n"]];
//append The Json string
[theBody appendString:myJsonString];
[theBody appendString:[NSString stringWithFormat:@"%@", boundary]];
//this appends the image
[theBody appendString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"data\"; filename=\"photo\""]];
[theBody appendString:[NSString stringWithFormat:@"Content-Type: image/png\r\n\r\n"]];
[theBody appendString:[NSString stringWithFormat:@"%@",imageData]];
[theBody appendString:[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] ];
[theRequest setHTTPBody:[theBody dataUsingEncoding:NSUTF8StringEncoding]];
안녕하세요 Mohit 고맙습니다. 문제는 내가 base64로 인코딩하면 파일 크기가 33 % 증가합니다. 이것이 제가 multipart를 찾고 있었던 이유입니다. 멀티 파트 요청 –
이봐, 나는 샘플을 "http://pastie.org/5452037"에 뒀다. 그것을 확인해보십시오. 문제가 해결 될지도 모릅니다. –
확인하셨습니까? 그것은 작동하지 않습니까? 저에게 알려주십시오. –