使用AFNetworking下载文件
2015-10-10 08:56
337 查看
.h文件
.m实现文件
typedef void(^CompletionDown)(BOOL isSuccess, NSError *error, NSString *path); /* 下载方法,参数: urlPath:下载地址, saveFileName:命名存储文件 */ +(void)downFileWithUrl:(NSString *)urlPath WithSavePath:(NSString *)saveFileName onCompletionDownLoad:(CompletionDown)block;
.m实现文件
+(void)downFileWithUrl:(NSString *)urlPath WithSavePath:(NSString *)saveFileName onCompletionDownLoad:(CompletionDown)block{ //获取沙盒路径 NSString *sanboxDicPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0]; //创建文件夹 NSString *directory = [sanboxDicPath stringByAppendingPathComponent:@"pdf"]; NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL fileExists = [fileManager fileExistsAtPath:directory]; NSError *directoryCreateError = nil; if (!fileExists) {//文件夹不存在,创建 [fileManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:&directoryCreateError]; } if (directoryCreateError) { block(NO,directoryCreateError,nil); return; } NSString *savePath = [directory stringByAppendingPathComponent:saveFileName]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.requestSerializer.timeoutInterval = 70; AFHTTPRequestOperation *op = [manager GET:urlPath parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"successful download to %@", savePath); block(YES,nil,savePath); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); block(NO,error,nil); }]; [op setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { NSLog(@"is download:%f", (float)totalBytesRead / totalBytesExpectedToRead); }]; op.outputStream = [NSOutputStream outputStreamToFileAtPath:savePath append:NO]; [op start]; }
相关文章推荐
- User Scripts: Video Download by User Scripts
- 《你是我的小羊驼》游戏ios源码
- Linux服务器加速——TrafficSqueezer安装过程
- Download:NORTON.INTERNET.SECURITY.2007
- PsTools v2.4 Download
- mydumper的安装使用
- unetbootin
- 利用SecureCRT在linux与Windows之间传输文件
- WebLogic下载_各个版本(WebLogic6、WebLogic7、WebLogic8、WebLogic9、WebLogic10、WebLogic11)
- Grml 2014.11 发布,Linux 发行版
- *.docx,*.xlsx,*.pptx文件下载后变成.zip文件,*.csv文件下载后变为.html文件
- AFNetwork 2.0在请求时报错code=-1016 和 3840
- Nginx自动安装脚本
- 用 Qt Creator环境 为 BlackBerry 10 开发 Qt应用
- PredictionIO 安装配置
- MariaDB - 所有版本下载
- redis安装部署
- wget下载https链接
- GNU gcc package link
- XShell 4使用sftp上传下载文件命令