利用ASIHttp实现文件的下载、上传和网络状态的监控
2013-11-09 23:02
811 查看
第一、设置下载路径
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];
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];
相关文章推荐
- 简介
- ASIHTTPRequest类库简介和使用说明
- ASIHTTPRequest
- IOS部署ASIHTTPRequest框架
- ASIHttpRequest "_UTTypeCopyPreferredTagWithClass", referenced from:
- iOS ASIHTTPRequest 多次重复请求的问题
- ASIHttpRequest使用详解(4)
- ASIHttpRequest使用详解(3)
- ASIHttpRequest使用详解(1)
- iOS ASIHTTPRequest详解
- ASIHTTPRequest简单使用
- ASIHTTPRequest
- iOS应用开发类库TSNetwork
- AFNetworking、MKNetworkKit和ASIHTTPRequest对比
- libxml/HTMLparser.h file not found
- ASIHttpRequest "_UTTypeCopyPreferredTagWithClass", referenced from:
- ASIHttpRequest中Post中文乱码解决
- ASIHttpRequest异步操作返回数据
- 去掉ASIHttpRequset中request在block的警告
- ASIHTTPRequest 应用小结