zoukankan      html  css  js  c++  java
  • 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==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
    //直接使用
    #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]; }
  • 相关阅读:
    6.1成果(冲刺2.10)
    5.31成果(冲刺2.9)
    5.30成果(冲刺2.8)
    5.29成果(冲刺2.7)
    5.28成果(冲刺2.6)
    5.27成果(冲刺2.5)
    5.26成果(冲刺2.4)
    5.25成果(冲刺2.3)
    Nginx location匹配后 跳转问题
    记一次centos上发布core,访问502的bug
  • 原文地址:https://www.cnblogs.com/gcb999/p/3262089.html
Copyright © 2011-2022 走看看