ios中文件下载(带缓存)
使用asiHttPRequst框架

封装下载类
#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 @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==)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:];
//设置下载缓存
[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
//直接使用
#import "MMViewController.h"
#import "AsynchDownloadFile.h" @interface MMViewController () @end @implementation MMViewController -(void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(FileDownloadComplete:) name:FILESDOWNLOADCOMPLETE object:nil];
} - (IBAction)click:(id)sender {
NSString *url=@"http://。。。。。?classid=0&filename=110908133300893.doc";
[[AsynchDownloadFile ShareTheme] DownLoadFileUrl:url];
} -(void)FileDownloadComplete:(id)sender{
NSLog(@"--com");
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
ios中文件下载(带缓存)的更多相关文章
- ios中asihttprequest 下载缓存
		asi中下载缓存第一种方法 #import <UIKit/UIKit.h> #import "ASIHTTPRequest.h" #import "ASIDo ... 
- ios中网络请求缓存
		#import <Foundation/Foundation.h> #import "ASIFormDataRequest.h" @protocol NetWorkde ... 
- IOS中经典的缓存对比
		http://bpoplauschi.wordpress.com/2014/03/21/ios-image-caching-sdwebimage-vs-fastimage/ 
- iOS中清除缓存的方法  以及SDWebimage自带的清除缓存方法
		1 SDWebimage中 (1) 计算缓存的大小 单位 : (MB) CGFloat size = [[SDImageCache sharedImageCache] getSize] / 102 ... 
- iOS中dyld缓存的实现原理是怎样的?
		在iOS开发中,为了提升系统的安全性,很多系统库文件都被打包到一个缓存的文件当中即dyld缓存,那大家对dyld缓存了解多少呢?今天小编将和大家分享的就是一位iOS大神对dyld缓存的使用分析,一起来 ... 
- IOS中无缓存的图片载入
		在IOS中,我们常用[UIImage imageNamed]方法获取图像,这种方法简便,容易理解.但是有个缺点,就是有缓存.这种方式 传人的图像的就是通过文件名方式文件名.如果,我们内存有限,我们就必 ... 
- Delphi中带缓存的数据更新技术
		一. 概念 在网络环境下,数据库应用程序是c/s或者是多层结构的模式.在这种环境下,数据库应用程序的开发应当尽可能考虑减少网络数据传输量,并且尽量提高并发度.基于这个目的,带缓存的数据更新技术应运而生 ... 
- iOS五种本地缓存数据方式
		iOS五种本地缓存数据方式 iOS本地缓存数据方式有五种:前言 1.直接写文件方式:可以存储的对象有NSString.NSArray.NSDictionary.NSData.NSNumber,数据 ... 
- iOS中的地图和定位
		文章摘自http://www.cnblogs.com/kenshincui/p/4125570.html#location 如有侵权,请联系删除. 概览 现在很多社交.电商.团购应用都引入了地图和定 ... 
随机推荐
- Permutations leetcode java
			题目: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the f ... 
- jQuery UI全教程之一(dialog的使用教程)
			jQuery UI目前的版本已经更新到了1.8.7.个人感觉和easyui相比起来,jQuery UI在界面的美观程度和可定制型更强一些.所以再次将一些jQuery UI组件的用法说明一下,方便日后查 ... 
- 条件随机场 (CRF) 分词序列谈之一(转)
			http://langiner.blog.51cto.com/1989264/379166 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.ht ... 
- vscode关闭后未打开上次界面的解决办法
			1.更新vscode至最新版,当前时间2017-06-20,vs最新版1.13.1 2.关闭vscode的正确方式,不是点击左上角的叉叉,而是CMD+Q或者在Dock右键退出 3.这样,你在下次打开v ... 
- 【树形DP】 HDU 2412 Party at Hali-Bula
			给出根节点(BOSS) 然后还有N-1个边 A B 由B指向A (B为A 的上司) 每次仅仅能选择这个关系中的当中一个 求最多选几个点 而且输出是不是唯一的 重点推断是否唯一: 1.若下属不去和去都 ... 
- advertisingIdentifier
			iOS 7后Mac 地址就不能用了. 不过可以用advertisingIdentifier来取,再多个project 里测试是唯一的,但如果遇到系统升级或是重刷这个就不一定能唯一了.. 这里还要加一个 ... 
- Jquery遍历筛选数组的几种方法和遍历解析json对象|Map()方法详解
			Jquery遍历筛选数组的几种方法和遍历解析json对象|Map()方法详解 一.Jquery遍历筛选数组 1.jquery grep()筛选遍历数组 $().ready( function(){ v ... 
- JAVA中使用Apache HttpComponents Client的进行GET/POST请求使用案例
			一.简述需求 平时我们需要在JAVA中进行GET.POST.PUT.DELETE等请求时,使用第三方jar包会比较简单.常用的工具包有: 1.https://github.com/kevinsawic ... 
- ububuntu配置ip和dns
			装完ubuntu 第一件事情就是连上网,换个源,进行更新操作,但前提条件是要配好ip和dns. 下面把自己配置的过程记录下来,权且当作一份备份,以便不时之需. 一.配置ip ubuntu的网络配置信息 ... 
- Docker镜像保存save、加载load
			(1)查看要要保存的镜像的ID [root@localhost docker]# docker images (2)保存镜像 [root@localhost docker]# docker save ... 
