我想将图像从设备上传到服务器。但它没有上传!甚至我的返回成功值也显示为空。成功值应该是 1 或 0。我的代码在这里给出。请告诉我我的代码是否有任何错误。在此先感谢您的帮助。
-(void)ImageUpload{
NSString *urlString = [NSString stringWithFormat:@"%@upload.php", APIheader];
NSString *postLength = [NSString stringWithFormat:@"%d", [imgDATA length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setURL:[NSURL URLWithString:urlString]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:imgDATA];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"return string: %@",returnString);
returnString = [returnString stringByReplacingOccurrencesOfString:@"(" withString:@"["];
returnString = [returnString stringByReplacingOccurrencesOfString:@")" withString:@"]"];
returnString = [returnString stringByReplacingOccurrencesOfString:@";" withString:@""];
SBJsonParser *parser = [[SBJsonParser alloc]init];
NSArray *array = (NSArray *)[parser objectWithString:returnString error:nil];
NSString *status = [NSString stringWithFormat:@"%@",[[array objectAtIndex:0]objectForKey:@"success"]];
if ([status isEqualToString:@"1"]) {
NSLog(@"Image Updated");
}
else{
NSLog(@"status is: %@",status);
}
}
[request release];
}