웹 서버에 사용자의 데이터를 업로드하는 앱이 있습니다.PHP를 사용하여 ios의 JSON을 통해 웹 서버에 이미지 업로드
아래 코드를 사용하여 아래 코드로 웹 서버에 데이터를 전달합니다.
-(IBAction)btnRegister:(id)sender
{
jsonSP = [SBJSON new];
jsonSP.humanReadable = YES;
NSString *service = @"/register.php";
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"register_btn.png"]);
[Base64 initialize];
NSString *imageString = [Base64 encode:imageData];
NSLog(@"%@",imageString);
NSString *requestString = [NSString stringWithFormat:@"{\"?username\"=\"%@\"&\"user_password\"=\"%@\"&\"first_name\"=\"%@\"&\"email\"=\"%@\"&\"profile_photo_link\"=\"%@\"\"}",@"Test",@"Test",@"Test",@"[email protected]",imageString];
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"URLName" ofType:@"plist"];
NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];
NSString *urlLoc = [fileContents objectForKey:@"URL"];
urlLoc = [urlLoc stringByAppendingString:service];
NSLog(@"URL : %@",urlLoc);
// urlLoc = [urlLoc stringByAppendingString:@"?username=abcd&password=abcd"];
urlLoc = [urlLoc stringByAppendingString:requestString];
urlLoc = [urlLoc stringByReplacingOccurrencesOfString:@"{" withString:@""];
urlLoc = [urlLoc stringByReplacingOccurrencesOfString:@"}" withString:@""];
urlLoc = [urlLoc stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSLog(@"URL : %@",urlLoc);
// [fileContents release]; //o_r
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: urlLoc]];
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
[request setHTTPMethod: @"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: requestData];
// self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
NSError *respError = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &respError ];
NSLog(@"response is %@",returnData);
// [request release];
if (respError)
{
//[customSpinner hide:YES];
UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"Internet connection is not Available!" message:@"Check your network connectivity" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alt show];
//[alt release];
}
else
{
NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(@"response string %@",responseString);
}
}
웹 서버에 이미지없이 성공적으로 데이터를 보낼 수 있습니다.
그러나 Base64 인코딩으로 위에 표시된 코드로 이미지를 업로드하면 데이터가 웹 서버에 업로드되지 않습니다. NSLog에서 요청 쿼리 문자열이 너무 길다는 오류가 발생합니다.
이미 Base64.h 및 .m 파일을 가져 왔습니다.
이미지와 함께 데이터를 전달하고 싶습니다.
어떻게하면됩니까?
나는 많은 링크가 있음을 알고 있지만이 방법으로 전달하고 싶습니다. 그래서 내가 뭘 잘못하고 있는지 말해 주시겠습니까?
도와주세요.
미리 감사드립니다.
고마워. 나는 그것을 확인하고 알게 될 것이다. – Manthan
NSLog에서 너무 긴 base64String을 얻었 기 때문에 요청 문자열의 오류 메시지가 여전히 너무 길다. – Manthan
다중 경로 데이터를 통해 이미지를 보내는 방법을 알고 있습니까? 그렇다면 도움을주십시오. 카이 네 바카이를 도우려는 자유로운 안녕. – Manthan