我需要在网络服务器中发布图像和文本。文本数据保存但图像不保存。然后我尝试了下面的代码。现在文本数据也没有保存。
NSData *imageData = UIImagePNGRepresentation(chosenImage);
NSString *requestString =[NSString stringWithFormat:@"UserId=%@&CategoryId=%@&Continent=%@&Country=%@&City=%@&Gender=%@&ImageName=%@&ImageData=%@&AgeRange=%@",UserId,CategoryId,continentTextfield.text,countrytextfield.text,citytextfield.text,gender,imagename,imageData,ageTextfield.text];
NSString *url=[NSString stringWithFormat:@"http://192.168.2.4:98/UserImage.svc/InsertFacialImage?%@",requestString];
//----------------------
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
// Create 'POST' MutableRequest with Data and Other Image Attachment.
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
//image
[postbody appendData:[NSString stringWithFormat:@"\r\n--%@\r\n",boundary]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; file=\"image\"; filename=\"%@.png\"\r\n", imageData]dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:imageData]];
[request setHTTPBody:postbody];
// -----------------------
NSURLConnection *connReq = [NSURLConnection connectionWithRequest:request delegate:self];
if (connReq) {
NSLog(@"Connection Sucessful");
receivedData = [[NSMutableData alloc] init];
}
else {
NSLog(@"failed");
}
[connReq start];