Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

ios - Upload pdf file (from document directory) through web service in iPhone?

I'm working in iPhone application, upload pdf file from document directory through Web Service like "http://www.publigeee.com/index.php?q=api/upload&uid=1&title=Sample&file=Upload Pdf file here", How to do that? Please help me.

Thanks in Advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've made this just some days ago. This code uses AFNetworking: https://github.com/AFNetworking/AFNetworking

Code on the iPhone side (it uploads a pdf and another text parameter, item)

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];

On the Php side:

// 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

Not sure if it's error free, because it has been taken from a longer source, so I've modified some pieces of code, but it's a good start


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...