iphone - 通过iPhone中的Web服务上传pdf文件(从文档目录)吗?

标签 iphone ios pdf upload

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

提前致谢

最佳答案

我几天前才做的。此代码使用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

不知道它是否没有错误,因为它是从较长的来源中获取的,所以我已经修改了一些代码,但这是一个好的开始

关于iphone - 通过iPhone中的Web服务上传pdf文件(从文档目录)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13399542/

相关文章:

iphone - Instruments Leaks - 不显示我的源代码

ios - 如何同时绘制到多个 CGLayer?

iphone - 以编程方式自动锁定 iphone/ipad

html - 如何在显示所有 pdf 页面的 iframe 中显示 pdf?

ruby-on-rails - 使用 Wicked PDF 将 PDF 附加到电子邮件时如何指定布局

iphone - UIView 的 -drawRect 是否为 : have to be called on the main thread?

ios - UI系统字体之间的区别

ios - 如何在我的ios应用程序中添加调查猴子ios sdk?

ios - 将现有的核心数据迁移到 iCloud

javascript - HTML,调整大小的嵌入 pdf