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];
封装下载类
.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];
相关文章推荐
- ASIHTTPRequest 缓存+下载文件到本地
- ASIHTTPRequest的使用——下载文件
- ASIHttpRequest 下载服务器上面的文件
- ios (ASIHTTPRequest)获取下载文件大小(size)
- ios (ASIHTTPRequest)获取下载文件大小(size)
- ASIHTTPRequest系列(二):文件下载
- ASIHTTPRequest下载文件
- ios中asihttprequest 下载缓存
- iOS 使用ASIHttpRequest和GData解析xml网络文件
- ASIHTTPRequest系列(二):文件下载
- 使用HttpWebRequest下载远程文件部分网站出现异常
- ASIHTTPRequest系列(二):文件下载
- ASIHTTPRequest系列(二):文件下载
- iphone开发--使用ASIHTTPRequest实现下载,断点续传功能
- ASIHTTPRequest系列(二):文件下载
- iPhone开发笔记(16)使用ASIHTTPRequest和ASIDownloadCache实现本地缓存
- ASIHTTPRequest系列(二):文件下载
- ASIHTTPRequest系列(二):文件下载
- ASIHTTPRequest系列(二):文件下载
- ios 使用ASIHTTPRequest异步读取远程txt文件