利用ASIHttp实现文件的下载、上传和网络状态的监控
来源:互联网 发布:中银淘宝校园卡欠费 编辑:程序博客网 时间:2024/05/29 17:32
第一、设置下载路径
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];
- 利用ASIHttp实现文件的下载、上传和网络状态的监控
- IOS网络笔记--ASIHTTP类库的使用(上传和下载)
- AFNetwork上传文件,下载文件,监控网络状态
- AFNetwork上传文件,下载文件,监控网络状态
- 利用WebService实现远程服务器文件的上传和下载
- 利用WebService实现远程服务器文件的上传和下载
- javaEE利用地方实现文件的上传和下载
- php实现文件的上传和下载
- servlet实现文件的上传和下载
- struts2 实现文件的上传 和 下载
- java实现文件的上传和下载
- WCF实现的文件上传和下载
- Struts2实现文件的上传和下载
- springMVC实现文件的上传和下载
- Struts2实现文件的上传和下载
- java实现文件的上传和下载
- java实现文件的上传和下载
- SpringMVC实现文件的上传和下载
- Dwr3.0与struts2一起使用的方法步骤详解
- JSF组件之selectManyCheckbox默认default 勾选checked
- openssl 的RSA 非对称加密算法
- Spring IoC容器--入门 二
- 求两个数的最大公约数和最小公倍数
- 利用ASIHttp实现文件的下载、上传和网络状态的监控
- 产品经理
- 敏捷软件开发
- 使用Mahout搭建推荐系统之入门篇3-Mahout源码初探
- chm文件的中文显示乱码问题解决
- ios UITextView
- SQL Server 2008 各种DateTime的取值范围
- Oracle数据库中如果存在表就删除不存在就创建的实例
- 黑马程序员---基础班毕业