使用 AFNetworking 下载文件
Posted
技术标签:
【中文标题】使用 AFNetworking 下载文件【英文标题】:Downloading a File with AFNetworking 【发布时间】:2013-10-11 13:24:16 【问题描述】:尝试为我的 iPhone 程序编写一个方法,该方法为文件提供 URL 地址,它将下载到 ios 应用程序的文档目录。
按照 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];
最终结果是文件成功下载到我的documents目录下,但是名字是乱码:
文件下载到: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"
最后,我需要做什么才能获得下载进度?
【问题讨论】:
【参考方案1】:这是我能够创建的答案:
-(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];
我愿意接受改进或建议:)
【讨论】:
AFHTTPRequestOperation 现在不可用? 当我下载视频并点击返回时没有下载完成。然后我再次点击视频,那个时候我得到这个错误=>MediaPlayerErrorDomain Code=-11800 ...所以请帮助我【参考方案2】:你可以简单地替换这一行:
return [documentsDirectoryPath URLByAppendingPathComponent:[targetPath lastPathComponent]];
与:
return [documentsDirectoryPath URLByAppendingPathComponent:@"fw4.pdf"];
因为[targetPath lastPathComponent]
返回类似CFNetworkDownload_60NWIf.tmp
【讨论】:
在这段代码中直接将视频名称保存在documnet目录中,没有成功的视频下载对吗?"=>NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent :pdfName]; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];以上是关于使用 AFNetworking 下载文件的主要内容,如果未能解决你的问题,请参考以下文章