标签:des blog http io 使用 ar for strong sp
转:http://zyc-to.blog.163.com/blog/static/17152400201110221114526/
从0.94版本开始,ASIHTTPRequest可以恢复中断的下载
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
- (IBAction)resumeInterruptedDownload:(id)sender{ NSURL *url = [NSURL URLWithString: ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; NSString *downloadPath = @"/Users/ben/Desktop/asi.png"; //当request完成时,整个文件会被移动到这里 [request setDownloadDestinationPath:downloadPath]; //这个文件已经被下载了一部分 [requestsetTemporaryFileDownloadPath:@"/Users/ben/Desktop/asi.png.download"]; [request setAllowResumeForFileDownloads:YES]; [request startSynchronous]; //整个文件将会在这里 NSString *theContent = [NSString stringWithContentsOfFile:downloadPath];} |
这个特性只对下载数据到文件中有效,你必须为一下情况的request设置allowResumeForFileDownloads 为YES:
另外,你必须自己设置一个临时下载路径(setTemporaryFileDownloadPath),这个路径是未完成的数据的路径。新的数据将会被添加到这个文件,当下载完成时,这个文件将被移动到downloadDestinationPath 。
断点续传的工作原理是读取temporaryFileDownloadPath的文件的大小,并使用Range: bytes=x HTTP头来请求剩余的文件内容。
ASIHTTPRequest并不检测是否存在Accept-Ranges头(因为额外的HEAD头请求会消耗额外的资源),所以只有确定服务器支持断点续传下载时,再使用这个特性。
标签:des blog http io 使用 ar for strong sp
原文地址:http://www.cnblogs.com/ygm900/p/4029589.html