ASIHTTPRequest下载示例(支持断点续传)
一、创建网络请求队列
首先,创建网络请求队列,如下:
ASINetworkQueue *que = [[ASINetworkQueue alloc] init];
self.netWorkQueue = que;
[que release];
[self.netWorkQueue reset];
[self.netWorkQueue setShowAccurateProgress:YES];
[self.netWorkQueue go];
二、创建存放路径
//初始化Documents路径
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//初始化临时文件路径
NSString *folderPath = [path stringByAppendingPathComponent:@"temp"];
//创建文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//判断temp文件夹是否存在
BOOL fileExists = [fileManager fileExistsAtPath:folderPath];
if (!fileExists) {//如果不存在说创建,因为下载时,不会自动创建文件夹
[fileManager createDirectoryAtPath:folderPath
withIntermediateDirectories:YES
attributes:nil
error:nil];
}
三、发送下载请求
这里对下面几个对象说明一下:CustomCell是我自定义的cell,cell上面有下载和暂停两个按钮,其tag值为cell所在的行,因此这里的[sendertag]为下载按钮的tag值,self.downloadArray为array数组对象,存放要下载的资源字典信息,在该字典中有一键为URL,它对应的值就是我们下载链接。
这些东西,根据自己的实际需要改动一下即可使用
CustomCell *cell = (CustomCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[sender tag] inSection:0]];
NSString *filePath = [[self.downloadArray objectAtIndex:[sender tag]] objectForKey:@"URL"];
NSLog(@"filePath=%@",filePath);
//初始下载路径
NSURL *url = [NSURL URLWithString:filePath];
//设置下载路径
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
//设置ASIHTTPRequest代理
request.delegate = self;
//初始化保存ZIP文件路径
NSString *savePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"book_%d.zip",[sender tag]]];
//初始化临时文件路径
NSString *tempPath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"temp/book_%d.zip.temp",[sender tag]]];
//设置文件保存路径
[request setDownloadDestinationPath:savePath];
//设置临时文件路径
[request setTemporaryFileDownloadPath:tempPath];
//设置进度条的代理,
[request setDownloadProgressDelegate:cell];
//设置是是否支持断点下载
[request setAllowResumeForFileDownloads:YES];
//设置基本信息
[request setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:[sender tag]],@"bookID",nil]];
NSLog(@"UserInfo=%@",request.userInfo);
//添加到ASINetworkQueue队列去下载
[self.netWorkQueue addOperation:request];
//收回request
[request release];
三、暂停请求
这里的cell下下载时的一样,
CustomCell *cell = (CustomCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[sender tag] inSection:0]];
for (ASIHTTPRequest *request in [self.netWorkQueue operations]) {
NSInteger bookid = [[request.userInfo objectForKey:@"bookID"] intValue];//查看userinfo信息
if ([sender tag] == bookid) {//判断ID是否匹配
//暂停匹配对象
[request clearDelegatesAndCancel];
}
}
四、ASIHTTPRequestDelegate回调方法
上面已经把下载请求与暂停请求实现,点击下载时,开始下载资源;当点暂停时,下载中断;当我们再点击下载按钮时,继续下载,在第二步的
[request setAllowResumeForFileDownloads:YES]设置是是否支持断点下载。下面要实现ASIHTTPRequestDelegate代理方法如下:
#pragma mark -
#pragma mark ASIHTTPRequestDelegate method
//ASIHTTPRequestDelegate,下载之前获取信息的方法,主要获取下载内容的大小,可以显示下载进度多少字节
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders {
NSLog(@"didReceiveResponseHeaders-%@",[responseHeaders valueForKey:@"Content-Length"]);
NSLog(@"contentlength=%f",request.contentLength/1024.0/1024.0);
int bookid = [[request.userInfo objectForKey:@"bookID"] intValue];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
float tempConLen = [[userDefaults objectForKey:[NSString stringWithFormat:@"book_%d_contentLength",bookid]] floatValue];
NSLog(@"tempConLen=%f",tempConLen);
//如果没有保存,则持久化他的内容大小
if (tempConLen == 0 ) {//如果没有保存,则持久化他的内容大小
[userDefaults setObject:[NSNumber numberWithFloat:request.contentLength/1024.0/1024.0] forKey:[NSString stringWithFormat:@"book_%d_contentLength",bookid]];
}
}
//ASIHTTPRequestDelegate,下载完成时,执行的方法
- (void)requestFinished:(ASIHTTPRequest *)request {
int bookid = [[request.userInfo objectForKey:@"bookID"] intValue];
CustomCell *cell = (CustomCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:bookid inSection:0]];
cell.downloadCompleteStatus = YES;
cell.progressView.progress = 0.0;
}
经过上述步骤,一个支持断点续传的功能程序实现了!
ASIHTTPRequest下载示例(支持断点续传)的更多相关文章
- Spring Boot Ftp Client 客户端示例支持断点续传
本章介绍 Spring Boot 整合 Ftpclient 的示例,支持断点续传 本项目源码下载 1 新建 Spring Boot Maven 示例工程项目 注意:是用来 IDEA 开发工具 File ...
- 打印 上一主题 下一主题 利用cURL实现单个文件分多段同时下载,支持断点续传(修订版)
利用cURL实现单个文件分多段同时下载,支持断点续传(修订版) [复制链接] 摘自 http://bbs.chinaunix.net/thread-917952-1-1.html 在ubuntu下 ...
- C# http下载(支持断点续传)
分享下项目里面自己封装的一个http下载类 功能如下: 1.支持断点续传 2.下载失败自动重试 3.超时等异常处理 using System; using System.Collections.Gen ...
- C# FTP上传下载(支持断点续传)
<pre class="csharp" name="code"><pre class="csharp" name=&quo ...
- Java实现多线程下载,支持断点续传
完整代码:https://github.com/iyuanyb/Downloader 多线程下载及断点续传的实现是使用 HTTP/1.1 引入的 Range 请求参数,可以访问Web资源的指定区间的内 ...
- c#上传下载ftp(支持断点续传)
using System;using System.Net;using System.IO;using System.Text;using System.Net.Sockets;namespace f ...
- 【FTP】FTP文件上传下载-支持断点续传
Jar包:apache的commons-net包: 支持断点续传 支持进度监控(有时出不来,搞不清原因) 相关知识点 编码格式: UTF-8等; 文件类型: 包括[BINARY_FILE_TYPE(常 ...
- php实现的支持断点续传的文件下载类
通常来说,php支持断点续传,主要依靠HTTP协议中 header HTTP_RANGE实现. HTTP断点续传原理: Http头 Range.Content-Range()HTTP头中一般断点下载时 ...
- php 支持断点续传的文件下载类
php 支持断点续传的文件下载类 分类: php class2013-06-30 17:27 17748人阅读 评论(6) 收藏 举报 php断点续传下载http测试 php 支持断点续传,主要依靠H ...
随机推荐
- math方法
1.丢弃小数部分,保留整数部分parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math.round(5/2) 4,向下取整 Math.fl ...
- 关于IP地址的一个细节问题
使用ip2long()和long2ip()函数把IP地址转成整型存放进数据库而非字符型.这几乎能降低1/4的存储空间.同时可以很容易对地址进行排序和快速查找;
- HTML之常用标签
一.H标签 标题(Heading)是通过<h1>-<h6>等标签进行定义的. <h1>定义最大的标题,<h6>定义最小的标题. 未完待续....
- document.compatMode属性和获取鼠标的位置
document.compatMode属性 document.compatMode用来判断当前浏览器采用的渲染方式. 官方解释: BackCompat:标准兼容模式关闭.CSS1Compat:标准兼容 ...
- [问题2015S03] 复旦高等代数 II(14级)每周一题(第四教学周)
[问题2015S03] 设 \(g(x)=x^n+a_1x^{n-1}+\cdots+a_{n-1}x+a_n\) 是数域 \(\mathbb{K}\) 上的多项式, \(V\) 是 \(\math ...
- 招聘一个靠谱的 iOS
近一年内陆续面试了不少人了,从面试者到面试官的转变让我对 iOS 招聘有了更多的感受.经过了前段时间的一大波面试,我们终于找到了志同道合的小伙伴,面试也暂时告一段落了.总结下面试人过程中的感受,你也可 ...
- unity4.6学习Ugui中文文档-------参考-UGUI Rect Transform
1 . Rect Transform Rect Transform 是 2D 与 3D 图形的 Transform 组件对应.它用来指定用户界面系统中的大小. 位置和旋转的控件 Properties ...
- Writing Text Files On The Client in Oracle Forms 10g
Below is the example to write file on client in Oracle Forms 10g with webutil library package.Note: ...
- 20个Linux服务器安全强化建议(一)
Linux服务器安全对于保护用户数据.知识产权非常重要,同时还能减少你面对黑客的时间.在工作中,通常由系统管理员对Linux的安全负责,在这篇文章中,介绍了20条对Linux系统进行强化的建议.本文所 ...
- (转载)U-boot启动完全分析
1.1 U-Boot工作过程 U-Boot启动内核的过程可以分为两个阶段,两个阶段的功能如下: (1)第一阶段的功能 Ø 硬件设备初始化 Ø 加载U-Boot第二阶段代码到RAM空间 Ø 设置好栈 Ø ...