使用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中文件下载(带缓存)的更多相关文章

  1. ios中asihttprequest 下载缓存

    asi中下载缓存第一种方法 #import <UIKit/UIKit.h> #import "ASIHTTPRequest.h" #import "ASIDo ...

  2. ios中网络请求缓存

    #import <Foundation/Foundation.h> #import "ASIFormDataRequest.h" @protocol NetWorkde ...

  3. IOS中经典的缓存对比

    http://bpoplauschi.wordpress.com/2014/03/21/ios-image-caching-sdwebimage-vs-fastimage/

  4. iOS中清除缓存的方法 以及SDWebimage自带的清除缓存方法

    1  SDWebimage中 (1)  计算缓存的大小 单位 : (MB) CGFloat size = [[SDImageCache sharedImageCache] getSize] / 102 ...

  5. iOS中dyld缓存的实现原理是怎样的?

    在iOS开发中,为了提升系统的安全性,很多系统库文件都被打包到一个缓存的文件当中即dyld缓存,那大家对dyld缓存了解多少呢?今天小编将和大家分享的就是一位iOS大神对dyld缓存的使用分析,一起来 ...

  6. IOS中无缓存的图片载入

    在IOS中,我们常用[UIImage imageNamed]方法获取图像,这种方法简便,容易理解.但是有个缺点,就是有缓存.这种方式 传人的图像的就是通过文件名方式文件名.如果,我们内存有限,我们就必 ...

  7. Delphi中带缓存的数据更新技术

    一. 概念 在网络环境下,数据库应用程序是c/s或者是多层结构的模式.在这种环境下,数据库应用程序的开发应当尽可能考虑减少网络数据传输量,并且尽量提高并发度.基于这个目的,带缓存的数据更新技术应运而生 ...

  8. iOS五种本地缓存数据方式

    iOS五种本地缓存数据方式   iOS本地缓存数据方式有五种:前言 1.直接写文件方式:可以存储的对象有NSString.NSArray.NSDictionary.NSData.NSNumber,数据 ...

  9. iOS中的地图和定位

    文章摘自http://www.cnblogs.com/kenshincui/p/4125570.html#location  如有侵权,请联系删除. 概览 现在很多社交.电商.团购应用都引入了地图和定 ...

随机推荐

  1. 《Hadoop大数据架构与实践》学习笔记

    学习慕课网的视频:Hadoop大数据平台架构与实践--基础篇http://www.imooc.com/learn/391 一.第一章 #,Hadoop的两大核心:     #,HDFS,分布式文件系统 ...

  2. Git-忽略规则.gitignore生效

    Git中如果忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法,如下这个文件每一行保存了一个匹配的规则例如,忽略单个文件或者整个目录的文件: *.css ...

  3. js el jstl list 循环

    需要在js中获取从Controller传过来的list集合,通过循环遍历找到对应的值,赋值到指定input框中 刚开始做法: for (var h = 0; h < gradesize; h++ ...

  4. 原生JS实现bind()函数

    一.bind()函数的两个特性: 1.bind和curring,函数科里化 function add(a, b, c) { var i = a+b+c; console.log(i); return ...

  5. 让App中增加LruCache缓存,轻松解决图片过多造成的OOM

    上次有过电话面试中问到Android中的缓存策略,当时模糊不清的回答,如今好好理一下吧. Android中普通情况下採取的缓存策略是使用二级缓存.即内存缓存+硬盘缓存->LruCache+Dis ...

  6. SQL 连接操作 及 查询分析

  7. Java-JUC(六):创建线程的4种方式

    Java创建线程的4种方式: Java使用Thread类代表线程,所有线程对象都必须是Thread类或者其子类的实例.Java可以用以下4种方式来创建线程: 1)继承Thread类创建线程: 2)实现 ...

  8. (文档)流媒体资源 Streaming Assets

    Most assets in Unity are combined into the project when it is built. However, it is sometimes useful ...

  9. node.js " The requested service provider could not be loaded or initialized"

    I'm trying to use any of the NodeJS or NPM commands but I always got the following error: socket: (1 ...

  10. C++ REST SDK i

    Welcome! The C++ REST SDK is a Microsoft project for cloud-based client-server communication in nati ...