使用AFNetworking下载文件

时间:2013-10-11 13:24:16

标签: iphone download afnetworking

尝试为我的iPhone程序编写一个给出文件URL地址的方法,它会下载到iOS App的文档目录。

在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];
}

最终结果是文件已成功下载到我的文档目录,但名称错误:

文件已下载到:file:///Users/myname/Library/Application%20Support/iPhone%20Simulator/7.0/Applications/25188DCA-4277-488E-B08A-4BEC83E59194/Documents/CFNetworkDownload_60NWIf.tmp

我期待的最终结果:

文件已下载到:file:///Users/myname/Library/Application%20Support/iPhone%20Simulator/7.0/Applications/25188DCA-4277-488E-B08A-4BEC83E59194/Documents/fw4.pdf

我使用cocoapods将AFNetworking添加到我的项目中: pod'AFNetworking',“〜> 2.0”

最后,我需要做些什么才能获得下载的进度?

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];
}

我愿意接受改进或建议:)

答案 1 :(得分:0)

只需更换此行:

return [documentsDirectoryPath URLByAppendingPathComponent:[targetPath lastPathComponent]];

使用:

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

因为[targetPath lastPathComponent]会返回类似 CFNetworkDownload_60NWIf.tmp

的内容