我正嘗試將圖像文件上傳到http服務器。這裏是代碼片段http文件上傳目標C
NSData *imgData = UIImagePNGRepresentation(img);
NSString *post = [NSString stringWithFormat:@"&UserID=%@&Query=%@&fileContent=", userID, @"putImage"];
NSMutableData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[postData appendBytes:[imgData bytes] length:imgData_len];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSURL *url = [NSURL URLWithString:@"http://ryan.verifyidonline.com/test.php"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[theRequest setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
此代碼只發送圖像文件的前幾個字符。我想我必須以某種方式對文件進行編碼,但我沒有嘗試過NSString編碼。