【问题标题】:Upload pdf file (from document directory) through web service in iPhone?通过iPhone中的网络服务上传pdf文件(从文档目录)?
【发布时间】:2012-11-04 04:07:52
【问题描述】:

我正在使用 iPhone 应用程序,通过 Web 服务从文档目录上传 pdf 文件,例如“http://www.publigeee.com/index.php?q=api/upload&uid=1&title=Sample&file=在此处上传 Pdf 文件“, 怎么做? 请帮帮我。

提前致谢

【问题讨论】:

  • 抬头向右,有一个搜索框-stackoverflow.com/questions/8564833/…
  • 这个链接只上传文本,但我想上传pdf文件
  • text 是数据,image 也是,PDF 也是,也许你可以根据你的情况调整答案?

标签: iphone ios pdf upload


【解决方案1】:

这是我几天前做的。此代码使用 AFNetworking:https://github.com/AFNetworking/AFNetworking

iPhone端的代码(它上传一个pdf和另一个文本参数,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];

在 PHP 方面:

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

不确定它是否没有错误,因为它取自更长的来源,所以我修改了一些代码,但这是一个好的开始

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-12
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多