您的位置:首页 > 移动开发 > IOS开发

ios开发之-从服务器下载文件

2015-09-01 21:34 525 查看
用于测试 在本地搭建了Apache 服务器
成功实现 从服务器下载文件
#pragma mark -文件下载-
-(void)downloadFile:(NSString *)urlStr{
AFURLSessionManager *manage = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
//存在中文 无法转成URL 需要转码
NSString *url = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//创建一个请求
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
__autoreleasing NSProgress *progress = nil;
NSURLSessionDownloadTask *task = [manage downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSLog(@"targetPath%@",targetPath);
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/代码.zip"];
return [NSURL fileURLWithPath:path];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if (error) {
NSLog(@"error%@",error);
}else{
NSLog(@"%@",filePath);
}
}];
[task resume];

_progress = progress;

//kvo
[_progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:nil];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: