我是iPhone开发的新手,我有一个问题如何将pdf文件的请求从文档目录发送到Web服务,如
http://www.publigeee.com/index.php?q=api/upload&uid=1&title=Sample&file=insert Pdf file here
如何在此链接中插入该pdf文件? 我花了5个小时,但我不能这样做,请帮帮我
先谢谢
答案 0 :(得分:1)
基本上您感兴趣的是上传文件,该文件存在于您的应用程序的文档目录中。在上述情况下,您需要将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。由于也希望其他Param喜欢标题等,你必须使用HTTP POST传递这些。
我不确定如果我能真正将我的答案与SO上的其他问题联系起来,this我的答案非常接近您的要求。
答案 1 :(得分:0)
您需要使用HTTP POST - 您无法向URL添加任意文件(大小会很快终止您的请求)。
查看从iOS发布HTTP的方法 - 有很多可用的信息。此外,您所指的网站(www.publicgee.com)必须在某些终点支持POST方法,否则您根本无法做到。
所以: