2013-10-11 159 views
2

試圖爲我的iPhone程序編寫一個給定文件URL地址的方法,它會下載到iOS應用程序的文檔目錄。使用AFNetworking下載文件

以下AFNetowrking的文檔,它似乎工作正常,但文件名總是一些垃圾。

我正在使用Xcode 5並將AFNetworking 2.0添加到我的項目中。下面的代碼,我到目前爲止有:

//#import "AFURLSessionManager.h" 

//On load (or wherever): 
[self downloadFile:@"http://www.irs.gov/pub/irs-pdf/fw4.pdf"]; 

-(void)downloadFile:(NSString *)UrlAddress 
{ NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

NSURL *URL = [NSURL URLWithString:UrlAddress]; 
NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) 
{ 
    NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]]; 
    return [documentsDirectoryPath URLByAppendingPathComponent:[targetPath lastPathComponent]]; 
} 
completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) 
{ 
    NSLog(@"File downloaded to: %@", filePath); 
}]; 
[downloadTask resume]; 
} 

最終的結果是,該文件被成功下載到我的文檔目錄,但有亂碼名稱:

文件下載到:文件:///用戶/ MYNAME /庫/ Application%20Support/iPhone%20Simulator/7.0 /應用/ 25188DCA-4277-488E-B08A-4BEC83E59194 /文檔/ CFNetworkDownload_60NWIf.tmp

最終的結果我很期待:

文件下載到:file:/// Users/myname/Library/Application% 20Support/iPhone%20Simulator/7.0 /應用/ 25188DCA-4277-488E-B08A-4BEC83E59194 /文檔/ fw4.pdf

我用的CocoaPods添加AFNetworking到我的項目: 莢 'AFNetworking', 「〜> 2.0」

最後,我需要做什麼才能獲得下載進度?

回答

5

這是我能夠創建答案:

-(void)downloadFile:(NSString *)UrlAddress 
{ 
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:UrlAddress]]; 
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
NSString *pdfName = @"The_PDF_Name_I_Want.pdf"; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:pdfName]; 
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO]; 

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"Successfully downloaded file to %@", path); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 
}]; 
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { 

    NSLog(@"Download = %f", (float)totalBytesRead/totalBytesExpectedToRead); 

}]; 
[operation start]; 
} 

我願意改進或建議:)

+0

AFHTTPRequestOperation現在不可用? – Sravan

+0

當我下載視頻並點擊返回時,下載完成。並再次點擊視頻,當我得到這個錯誤=> MediaPlayerErrorDomain代碼= -11800 ...所以plz幫助我 –

0

只要你可以替換該行:

return [documentsDirectoryPath URLByAppendingPathComponent:[targetPath lastPathComponent]]; 

有:

return [documentsDirectoryPath URLByAppendingPathComponent:@"fw4.pdf"]; 

si nce [targetPath lastPathComponent]返回類似於CFNetworkDownload_60NWIf.tmp

+0

inthis代碼直接保存視頻名稱在​​documnetdirectory沒有成功的視頻下載rght?「=> NSArray * paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask,YES); NSString * path = [[paths objectAtIndex:0] stringByAppendingPathComponent:pdfName]; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO]; –