使用AFNetworking下载文件
试图为我的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”
最后,我需要做什么才能获得下载进度?
这是我能够创建答案:
-(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];
}
我愿意改进或建议:)
只要你可以替换该行:
return [documentsDirectoryPath URLByAppendingPathComponent:[targetPath lastPathComponent]];
有:
return [documentsDirectoryPath URLByAppendingPathComponent:@"fw4.pdf"];
si nce [targetPath lastPathComponent]
返回类似于CFNetworkDownload_60NWIf.tmp
inthis代码直接保存视频名称在documnetdirectory没有成功的视频下载rght?“=> NSArray * paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask,YES); NSString * path = [[paths objectAtIndex:0] stringByAppendingPathComponent:pdfName]; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO]; –
AFHTTPRequestOperation现在不可用? – Sravan
当我下载视频并点击返回时,下载完成。并再次点击视频,当我得到这个错误=> MediaPlayerErrorDomain代码= -11800 ...所以plz帮助我 –