iphone - 将 pdf 文件请求从文档目录发送到 iPhone 中的(POST 方法)Web 服务?

标签 iphone ios web-services pdf

我是 iPhone 开发的新手,我有一个问题如何从文档目录向 Web 服务发送对 pdf 文件的请求,例如

http://www.publigeee.com/index.php?q=api/upload&uid=1&title=Sample&file=insert Pdf file here 

如何在此链接中插入该 pdf 文件? 我为此花了 5 个小时,但我做不到,请帮助我

提前致谢

最佳答案

本质上,您感兴趣的是上传应用程序文档目录中存在的文件。在上述情况下,您需要将 PDF 文件上传到您的服务器。

我使用以下方法为 XML 文件实现了相同的方法 -

//该函数假设文件存在于文档目录中

- (void)uploadXMLFile:(NSString*)sourceFileName atPath:(NSString*)filePath{


    // constructing connection request for url with no local and remote cache data and timeout seconds
    NSURL* scriptURL = [NSURL URLWithString:kFileUploadScriptURL];


    NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:scriptURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:20];

    [request setHTTPMethod:@"POST"];

    NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));

    // allocation mem for body data
    NSMutableData* bodyData = [[[NSMutableData alloc] init] autorelease];

    // initializing post body boundary
    NSString *stringBoundary = @"0xKhTmLbOuNdArY";

    // setting request header required
    // request containing multi part form data body and identified charater set encoding
    [request setAllHTTPHeaderFields:[NSDictionary dictionaryWithObjectsAndKeys:
                                     [NSString stringWithFormat:@"multipart/form-data; charset=%@; boundary=%@", charset, stringBoundary],@"Content-Type",
                                     @"gzip",@"Accept-Encoding",nil]];

    // appending body string
    [bodyData appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // Adds post data

    // Adds files to upload
    // file data identify its content type, data encoding and content disposition 
    // file is identified through its file name on server 
    // file data is retrived from the identified file url

    [bodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", @"file", sourceFileName] dataUsingEncoding:NSUTF8StringEncoding]];
    [bodyData appendData:[[NSString stringWithFormat:@"Content-Type: %@; charset=%@\r\n\r\n", @"document/xml", charset] dataUsingEncoding:NSUTF8StringEncoding]];

    [bodyData appendData:[NSData dataWithContentsOfFile:filePath]];

    [bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // set post body to request
    [request setHTTPBody:bodyData];

    NSString* bodyDataString = [[NSString alloc] initWithData:bodyData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",bodyDataString);

    // create new connection for the request
    // schedule this connection to respond to the current run loop with common loop mode.

    //  NSURLResponse *response = nil;                    
    //  NSError *error = nil;
    //  NSLog(@"%@",[[[NSString alloc] initWithData: [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error] encoding:NSUTF8StringEncoding]autorelease]);

    [[NSURLConnection alloc] initWithRequest:request  delegate:self];
    if (!responseData) {
        self.responseData = [[[NSMutableData alloc] initWithLength:0] autorelease];
    }
}

其中 kFileUploadScriptURL 是服务器 URL 的常量

#define kFileUploadScriptURL @"SERVER_ADDRESS/upload_file.php"

upload.php 本质上包含一个 PHP 脚本来接受数据并将其写入服务器上的目录之一。这样一个XML文件就上传到服务器了。您可以在函数中替换所需的参数来设置它上传PDF。由于还希望其他参数,如标题等,您必须使用 HTTP POST 传递这些参数。

我不确定我是否真的可以将我的答案与关于 SO 的其他问题联系起来,但是 this一个非常接近您的要求。

关于iphone - 将 pdf 文件请求从文档目录发送到 iPhone 中的(POST 方法)Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13411302/

相关文章:

jquery - Ajax jquery 调用获取 NetworkError : 403 Forbidden error in response

iphone - iOS4 - iPhone模拟器的背景音频

iphone - iOS 静态库 .a 文件未包含

ios - 在大小更改时使 Collection View 布局无效的正确方法

objective-c - 有没有办法跟踪仍然指向对象的内容?

java - 使用 Eclipse、JBoss 7.1.1 和 Jersey 1 获取 ClassNotFoundException

android - wordpress - 如何将所有内容和页面的 Web 服务与 Android 应用程序集成

iphone - iPhone 开发的最低规范笔记本电脑?

iphone - 如何在没有键的情况下设置 NSDictionary

iphone - 如何使用 ipad 文本字段中输入的值分配数组