[英]Upload pdf file (from document directory) through web service in iPhone?
[英]Send a request for pdf file from document directory to (POST Method) web service in iPhone?
我是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。 由於還希望使用其他Param(例如title等),因此必須使用HTTP POST傳遞這些參數。
我不確定我是否可以將我的答案與其他有關SO的問題聯系起來,但是這個問題確實與您的要求很接近。
您需要使用HTTP POST-您不能向URL添加任意文件(大小會很快殺死您的請求)。
尋找從iOS進行HTTP發布的方法-這里有很多可用的信息。 另外,您所指的網站(www.publicgee.com)必須在某個端點上支持POST方法,否則您將無法做到這一點。
所以:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.