2012-11-15 2 views
1

저는 iPhone 응용 프로그램에서 일하고 있습니다. 문서 디렉토리에서 웹 서비스를 통해 "http://www.publigeee.com/index.php?q=api/upload"와 같은 & uid = 1 & 제목 = 샘플 & 파일 = 여기에 PDF 파일 업로드 ", 어떻게해야합니까? 도와주세요.iPhone의 웹 서비스를 통해 pdf 파일 업로드 (문서 디렉토리에서)?

감사의 말

+2

검색 상자가 있습니다. http://stackoverflow.com/ 큐 유익한/8564833/ios-upload-image-text-using-http-post –

+0

답장을 보내 주셔서 감사합니다. – SampathKumar

+0

이 링크는 텍스트 만 업로드하지만 pdf 파일에 업로드하길 원합니다 – SampathKumar

답변

4

나는 이것을 불과 며칠 전에 만들었습니다. 이 코드는 AFNetworking 사용 : 아이폰 측에 https://github.com/AFNetworking/AFNetworking

코드를 PHP는 측면에서

NSData *itemData = [NSData dataWithContentsOfFile:filePath]; // filePath is the path of your PDF. This is an NSData containing your pdf file 
NSString *itemValue = @"A Text Item"; // another text item to upload if you need, like a description ecc... 

NSString *uploadURL = @"http://www.baseurl.it/"; // this is your upload url, the main site where you have your php file to receive the data 
NSURL *url = [[NSURL alloc]initWithString:uploadURL]; 
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url]; 
NSDictionary *dict = [NSDictionary 
         dictionaryWithObjects:@[itemValue] 
         forKeys:@[@"item"]]; 

// @"path/to/page.php" is the path to your php page receiving data 
NSURLRequest *request = [httpClient multipartFormRequestWithMethod: @"POST" 
        path:@"path/to/page.php" 
       parameters:dict 
constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) { 
     if (itemData) 
     { 
      [formData appendPartWithFileData:itemData 
             name:@"pdfData" 
            fileName:@"pdfData.pdf" 
            mimeType:@"application/pdf"]; 
     } 
}]; 

// sorry for the formatting here, is a long method using blocks 
AFJSONRequestOperation *json = [AFJSONRequestOperation JSONRequestOperationWithRequest:request 
        success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
          NSLog(@"Upload Success"); 
        } 
        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
          NSLog(@"Error: %@", error.description); 
        }]; 
[json start]; 

(그것은 PDF 및 다른 텍스트 매개 변수 항목을 업로드) : 그것의 경우

// file upload 
try { 
    $fileName = $_FILES['pdfData']['name']; 
    $tmpName = $_FILES['pdfData']['tmp_name']; 
    $fileSize = $_FILES['pdfData']['size']; 
    $fileType = $_FILES['pdfData']['type']; 

    $fp  = fopen($tmpName, 'r'); 
    $content = fread($fp, filesize($tmpName)); 
    $content = addslashes($content); 
    fclose($fp); 

    if(!get_magic_quotes_gpc()) 
    { 
     $fileName = addslashes($fileName); 
    } 

} catch (Exception $e) { 
    echo json_encode("Error:".$e); 
} 

// $content contains your pdf and you can save it wherever you want 

확실하지 않음 오류가 발생하지 않았으므로 더 긴 소스에서 가져 왔으므로 일부 코드를 수정했지만 좋은 시작입니다.

+0

답장을 보내 주셔서 감사합니다 – SampathKumar

+0

안녕하세요 – SampathKumar

+0

무엇이 질문입니까? :-) – LombaX

관련 문제