利用AFNetworking下载文件

//FileName是在沙盒的document里面新建文件夹的名字

        NSString *fileName = [NSStringstringWithFormat:@"%@%@",self.currentUser.name,self.currentUser.userId];

        NSFileManager *fileManger = [NSFileManagerdefaultManager];

        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:fileName];


判断路径中是否存在存在文件,如果不存在,就下载文件

        if (![fileMangerfileExistsAtPath:[path stringByAppendingPathComponent:documentAttachment.fileName]]) {    

            //创建相应的文件夹

            [fileManger createDirectoryAtPath:pathwithIntermediateDirectories:YESattributes:nilerror:nil];

            

            NSURLSessionConfiguration *configuration = [NSURLSessionConfigurationdefaultSessionConfiguration];

            AFURLSessionManager *manger = [[AFURLSessionManageralloc] initWithSessionConfiguration:configuration];

path是下载文件的路径

            NSURL *url = [NSURLURLWithString:Path];

            NSURLRequest *request = [NSURLRequestrequestWithURL:url];

            NSURLSessionDownloadTask *downLoadTask = [mangerdownloadTaskWithRequest:request progress:nildestination:^NSURL *(NSURL *targetPath,NSURLResponse *response) {

//返回要保存路径

                NSURL *downloadURL = [[NSFileManagerdefaultManager] URLForDirectory:NSDocumentDirectoryinDomain:NSUserDomainMaskappropriateForURL:nilcreate:YESerror:nil];

                downloadURL = [downloadURL URLByAppendingPathComponent:fileName];

                return [downloadURLURLByAppendingPathComponent:documentAttachment.fileName];

            } completionHandler:^(NSURLResponse *response,NSURL *filePath, NSError *error) {

                NSLog(@"File downloaded to: %@", filePath);

            }];

            [progressView setProgressWithDownloadProgressOfTask:downLoadTaskanimated:YES];

            [downLoadTask resume];

            

        }

        });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值