mysql 데이터베이스에 다른 세부 정보가있는 이미지를 삽입하고 싶습니다. 데이터 값을 저장할 수 있지만 이미지가 삽입되지 않습니다. 아래 코드를 확인하십시오.mysql 데이터베이스에 이미지를 삽입하는 방법
(IBAction) addData:(id)sender; {
//Image upload
NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);
NSString *urlString = @"http://localhost/myservice.php?";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Image upload string%@",returnString);
//Upload data
NSString *url = [NSString stringWithFormat:[urlString stringByAppendingString:@"userName=%@&age=%@&phone=%@&image=%@"],txtName.text, txtAge.text, txtPhone.text,returnString];
NSData *dataUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
NSString *strResult = [[NSString alloc] initWithData:dataUrl encoding:NSUTF8StringEncoding];
NSLog(@"%@",strResult);
if (![strResult isEqual: @""]) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Record Saved"
message: @"Your Record is saved successfully"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
//Clear fields
[email protected]"";
[email protected]"";
[email protected]"";
imageView.image= nil;
}else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Record not Saved"
message: @"Your Record does not saved successfully"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
위의 Objective-C 코드가 PHP 코드가 아닌 것으로 의심되는 이유가 있습니까? 잘못된 것만 보이는 것은'HTTPBody'의 시작 부분에있는 선두의'\ r \ n '입니다. [Charles] (http://charlesproxy.com)와 함께이 요청을 잘 보일 수도 있지만 여기에는 명백하게 잘못된 점이 하나도 없습니다. 물론 동기식 요청을 사용해서는 안되며 추가 NSData 객체를 인스턴스화해서는 안되며 JPEG 표현을 검색하지 않는 것이 좋습니다. 그러나 그 중 어느 것도 거래 차단기가 아닙니다. – Rob
FWIW, 이것은 이전에 사용한 코드입니다. http://stackoverflow.com/a/24252378/1271826 – Rob
Rob..please 내 PHP 코드도 확인하십시오. – user3259655