//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];
}
});