您的位置:首页 > 理论基础 > 计算机网络

ASIHTTPRequest的使用——文件下载(带缓存)

2014-05-16 09:18 148 查看
使用asiHttPRequst框架



封装下载类

.h文件
#import <Foundation/Foundation.h>

#define FILESDOWNLOADCOMPLETE @"FilesDownloadComplete" //文件下载完成

@interface AsynchDownloadFile : NSObject

+ (AsynchDownloadFile *)ShareTheme;

- (void)DownLoadFileUrl:(NSString *)aFileUrl;

@end

#import "AsynchDownloadFile.h"
#import "ASIHTTPRequest.h"
#import "ASIDownloadCache.h" //设置缓存类

@interface AsynchDownloadFile ()

@end

.m文件
@implementation AsynchDownloadFile

#pragma mark -单例

+ (AsynchDownloadFile *)ShareTheme
{
static dispatch_once_t onceToken;
static AsynchDownloadFile *loadFile = nil;
dispatch_once(&onceToken, ^{
loadFile = [[AsynchDownloadFile alloc] init];
});
return loadFile;
}

- (void)DownLoadFileUrl:(NSString *)aFileUrl
{
//
首先判断请求连接有没有文件
NSString *cacheFile = [self cacheFileForImage:aFileUrl];
NSFileManager *fgr = [NSFileManager defaultManager];
if ([fgr fileExistsAtPath:cacheFile])
{
//文件存在,直接发送消息
[[NSNotificationCenter defaultCenter] postNotificationName:FILESDOWNLOADCOMPLETE object:cacheFile];
}
else
{
//下载文件
可以清楚缓存
[self loadImageFromURL:[NSURL URLWithString:aFileUrl] imgInfoDic:nil];
}
}

#pragma mark -通过请求得到文件全路径
- (NSString *)cacheFileForImage:(NSString *)imgName
{
//
指定缓存文件路径
NSString *cacheFolder = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
cacheFolder = [cacheFolder stringByAppendingPathComponent:@"新闻类附件"];

NSFileManager *fmgr = [NSFileManager defaultManager];
if (![fmgr fileExistsAtPath:cacheFolder])
{
//如果文件夹不存在,创建
NSError *error = nil;
[fmgr createDirectoryAtPath:cacheFolder withIntermediateDirectories:YES attributes:nil error:&error];
if (error)
{
NSLog(@"创建缓存文件夹失败");
return nil;
}
}

//
文件名字以等号分割
NSArray *paths = [imgName componentsSeparatedByString:@"="];
if (paths.count == 0)
{
return nil;
}
return [NSString stringWithFormat:@"%@/%@",cacheFolder,[paths lastObject] ];
}

#pragma mark -下载文件
并且附带设置缓存

- (void)loadImageFromURL:(NSURL*)aUrl imgInfoDic:(NSDictionary*)infoDic
{
__block ASIHTTPRequest *request = nil;
if (aUrl)
{
request = [ASIHTTPRequest requestWithURL:aUrl];
[request setDelegate:self];
[request setTimeOutSeconds:60];
//设置下载缓存
[request setDownloadCache:[ASIDownloadCache sharedCache]];
//设置缓存存储策略
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy | ASIFallbackToCacheIfLoadFailsCachePolicy];

//设置缓存保存数据时间
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; //永久保存
[request setShouldContinueWhenAppEntersBackground:YES]; //设置后台运行。
[request setDownloadDestinationPath:[self cacheFileForImage:[aUrl absoluteString]]]; //设置缓存路径
}
else
{
return;
}

[request setCompletionBlock:^{
[[NSNotificationCenter defaultCenter] postNotificationName:FILESDOWNLOADCOMPLETE object:[self cacheFileForImage:[aUrl absoluteString]]];
}];

[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"error reason: %@", error);
}];

[request startAsynchronous];
}

@end

使用
1导入头文件
#import "AsynchDownloadFile.h"
2添加通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(FileDownloadComplete:) name:FILESDOWNLOADCOMPLETE object:nil];
3创建请求
NSString *url=@"http://。。。。。?classid=0&filename=110908133300893.doc";
[[AsynchDownloadFile ShareTheme] DownLoadFileUrl:url];
4通知回调
- (void)FileDownloadComplete:(id)sender
{
NSLog(@"--com");
}
5通知释放
[[NSNotificationCenter defaultCenter] removeObserver:self];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: