2012-03-06 4 views
1

먼저 PHP로 전송하기 위해 바이너리 데이터에있는 UIImage의있는 NSData을 변경는 어떻게 내 관련 코드를 보여

NSString *urlString = @"http://136.206.46.10/~katie_xueke/test.php"; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init]autorelease]; 
[request setURL:[NSURL URLWithString:urlString]]; 
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; 
[request setHTTPShouldHandleCookies:NO]; 
[request setTimeoutInterval:30.0f]; 

[request setHTTPMethod:@"POST"]; 

는 다음 나는 NSMutableData를 썼다 :

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
NSMutableData *body = [NSMutableData data]; 

//Image 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name =\"image\";filename=\"%@\"\r\n",imageName] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Type:application/octet-stream\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Length: %@\r\n",postLength]dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:imageData]]; 
[body appendData:[[NSString stringWithFormat:@"--%@--",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

$$ 질문은 :있는 NSData가 콘솔 창에 보여 주었다 서버 측과 신체 부위에 보낼 수없는 것 같이이다 :

Content-Disposition: form-data; name ="image";filename="2012:03:06 15:06:48" 

Content-Type:application/octet-stream 



Content-Length: 164692 

‰PNG 

을하기 위해 바이너리 데이터에있는 UIImage의있는 NSData을 변경하는 방법 PHP에 보내 ??

PS : 나는 UINT8 방법

UInt8 *rawData = [imageData bytes]; 

을 시도하지만에서 iOS 5가 사용되지 않는 것 같다. PHP는 측면에서

는 :

$uploaddir = './upload/';  
echo "recive a image";  
$file = basename($_FILES['userfile']['name']);  
$uploadfile = $uploaddir . $file;  

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {  
    echo "/uploads/{$file}";  

}

나는 다른 곳에서 그것을 복사 나는 웹 페이지 내 POST 이미지를 표시하는 방법을 모르겠어요.

누군가 나를 도울 수 있습니까?

감사합니다.

+0

당신은 제대로 데이터에 이미지를 변환 않았다하지만 당신은 문자열로 바이너리를보기 위해 노력하고 있습니다. 이 출력은 PNG 헤더의 일부이며 널 문자가 발생하기 때문에 인쇄가 중지됩니다. 데이터를 덤프하려면 16 진수를 사용해야합니다. ('NSLog' NSData' 객체는 16 진수 덤프를 수행합니다) – Joe

+0

PHP를 사용하여 이미지를 표시하는 방법을 알고 있습니까? –

+0

'-bytes'는 확실히 더 이상 사용되지 않습니다. 그것은 클래스의 프리미티브입니다! 무엇이 당신을 믿게 만들었습니까? – kperryua

답변

2

마지막으로 나는 혼자 해결책을 찾아 냈습니다.

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://XXXX"]]; 
NSMutableURLRequest *myRequest = [client multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
    [formData appendPartWithFileData:imageData name:@"uploadedfile" fileName:dateTime mimeType:@"images/png"]; 
}]; 
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:myRequest]; 
[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) { 

    NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite); 

}]; 
[operation setCompletionBlock:^{ 
    NSLog(@"response string: %@", operation.responseString); //Lets us know the result including failures 
}]; 

NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease]; 
[queue addOperation:operation]; 

나는 ASIHttpRequest 대신 AFNetworking을 사용했습니다. PHP는 측면에서

, 내 코드 :

<?php 

$filename="uploaded"; 
$target_path = "uploads/"; 

$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). 
    " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
} 
?> 

그리고 당신이 나에게 도움이 사람을 감사드립니다.

PS : 감사 실제로이 날 도움이 사람에 대한 많은 : http://6foot3foot.com/developer-journal/afnetworking-php

관련 문제