第一、设置下载路径
NSString *string=@"http://free2.macx.cn:81/tools/system/CleanMyMac-v1-10-8.dmg";
NSString *document=[NSHomeDirectory()stringByAppendingString:@"document"];
NSString *path=[documentstringByAppendingString:[stringlastPathComponent]];
ASIHTTPRequest *request=[ASIHTTPRequestrequestWithURL:[NSURLURLWithString:string]];
request.delegate=self;
第二、开始下载
[request setDownloadDestinationPath:path];
//下载代理进度条,还有上传代理进度条
request.downloadProgressDelegate=progressView;
[request startAsynchronous];
第三、利用block方法获取文件的大小
[request setHeadersReceivedBlock:^(NSDictionary *responseHeaders){
//响应头
NSLog(@"the length is %@",[responseHeadersobjectForKey:@"Content-Length"]);
}];
第四、获取下载进度
//通过kvo监听进度条
[progressView addObserver:selfforKeyPath:@"progress"options:NSKeyValueObservingOptionNewcontext:nil];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"%@",change);
NSNumber *num=[changeobjectForKey:@"new"];
float progress=[num floatValue];
NSLog(@"the len is %f %%",progress*100);
}
第五、网络状态的监控@property (nonatomic,retain)Reachability *reachAble;
//添加通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachStatusChanged:) name:kReachabilityChangedNotification object:nil];
self.reachAble=[Reachability reachabilityForInternetConnection];
//当网络状态发生改变时调用此函数
-(void)reachStatusChanged:(NSNotification *)notification
{
NetworkStatus status=self.reachAble.currentReachabilityStatus;
[self checkNetWork:status];
}
//网络状态的三种表示
kNotReachable:表示没有网络
kReachableViaWWAN:表示2G/3G
kReachableViaWIFI:标示WIFI
-(void)checkNetWork:(NetworkStatus) status
{
if (status==kNotReachable) {
NSLog(@"没有网络");
}else if (status==kReachableViaWWAN)
{
NSLog(@"2G/3G");
} else{
NSLog(@"WIFI");
}
}
第六、上传文件NSString *a = @"http://www.****.php";
NSURL *url = [NSURL URLWithString:a];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
[request setFile:file forKey:@"photo"];
//------------------------断点续传-----------------------
//设置是否支持断点续传
[request setAllowResumeForFileDownloads:YES];
NSString *tempPath = [NSHomeDirectory()stringByAppendingPathComponent:@"tmp/cache.download"];
//设置下载过程中暂存的文件路径
[request setTemporaryFileDownloadPath:tempPath];
[request startAsynchronous];