2012-04-25 68 views
4

我下載PDF文檔從服務器:如何下載數據保存到設備的文件系統

- (void)downloadSingleDocument:(NSURL *)url 
{ 
    [pdfData release]; 
    pdfData = [[NSMutableData alloc] init]; 
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; 
    [req addValue:@"Basic **************=" forHTTPHeaderField:@"Authorization"]; 
    downloadConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES]; 
} 

- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data 
{ 
    NSLog(@"Connection did receive data"); 
    [pdfData appendData:data]; 
} 

在connectionDidFinishLoading我想保存下載的PDF文件中的文件目錄到文件系統文件名相同,因爲它是在服務器。 做什麼是最好的方法?

回答

5

如果使用這個方法,你有大量的文件:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response 
{ 
    filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:save_name]; 
    [[NSFileManager defaultManager] createFileAtPath:filepath contents:nil attributes:nil]; 
    file = [[NSFileHandle fileHandleForUpdatingAtPath:filepath] retain];// Here file is object of NSFileHandle and its declare in .h File 

    [file seekToEndOfFile]; 

} 



- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 

[file seekToEndOfFile]; 

[file writeData:data]; 
} 


- (void)connectionDidFinishLoading:(NSURLConnection*)connection 
{ 
    [file closeFile]; 
// After you download your data. you can copy your data from here to filesystem. and remove from here 
} 
+0

「//在您下載數據後,您可以將您的數據從這裏複製到文件系統,並從這裏刪除」您可以詳細描述這個過程嗎?如何將它複製到文件系統然後刪除它? – Oleg 2012-04-26 07:24:22

+0

@ Oleg對不起,延遲迴復。看到這個:http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/NSFileManagerDelegate_Protocol/Reference/Reference。html#// apple_ref/doc/uid/TP40009948 – Hector 2012-04-26 09:18:37

+0

謝謝!這是我正在尋找的工作解決方案!接受答案! – Oleg 2012-04-26 10:02:33

-1

這應該這樣做

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSString *documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 

    NSString *filename = [con.originalRequest.URL lastPathComponent]; 
    NSString *path = [documentsDirectory stringByAppendingString:filename]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    [fileManager createFileAtPath:path contents:pdfData attributes:nil]; 

} 
+1

至於下載PDF文件的通用策略,這也不是很大。您將整個文件下載到RAM然後將其串行化到磁盤。這意味着大型PDF會在應用程序耗盡內存時崩潰。 – Jim 2012-04-25 11:41:27

+0

另外,如果要將默認文件管理器存儲在局部變量中,那麼最好在該方法的開始處執行該操作,並消除多餘的defaultManager調用中的一個。 – Jim 2012-04-25 11:42:29

+0

我得到異常終止應用程序由於未捕獲異常'NSInvalidArgumentException',原因:' - [NSURL stringByAppendingString:]:無法識別的選擇器發送到實例0xee91c00'。可能我必須使用absoluteString將NSURL轉換爲NSString。你也可以請建議如何解碼文件名從「file%20name.pdf」到「file name.pdf」? – Oleg 2012-04-25 11:51:03

2

使用NSURLDownload

對不起,只適用於Mac,不適用於iOS。

創建一個臨時文件,並在connection:didReceiveData:附加接收到的數據。在connectionDidFinishLoading:中,將文件移動到正確的位置,並在connection:didFailWithError:中刪除該臨時文件。

+0

但是我認爲NSURLDownload不適用於iOS ...這就是這個鏈接所說的...糾正我,如果我我錯了 – 2012-04-25 13:11:51

+0

啊,當然,我的錯誤。現在更新答案。 – Jim 2012-04-25 13:17:05

+0

哈哈..好...但我不知道OLEG如何回答:) – 2012-04-25 13:33:17

相關問題