ios大文件下载
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ios大文件下载,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2875字,纯文字阅读大概需要5分钟。
内容图文
1 #import "HMViewController.h" 2 3@interface HMViewController () 4 @property (weak, nonatomic) IBOutlet UIProgressView *progressView; 5/** 6 * 写数据的文件句柄 7*/ 8 @property (nonatomic, strong) NSFileHandle *writeHandle; 9/** 10 * 当前已下载数据的长度 11*/ 12 @property (nonatomic, assign) longlong currentLength; 13/** 14 * 完整文件的总长度 15*/ 16 @property (nonatomic, assign) longlong totalLength; 17 18 - (IBAction)start; 19@end 20 21@implementation HMViewController 22 23 - (void)viewDidLoad 24{ 25 [super viewDidLoad]; 26 27} 28 29 - (IBAction)start { 30// 检测一个文件\文件夹是否存在 31// [[NSFileManager defaultManager] fileExistsAtPath:<#(NSString *)#>]; 32 33// 获得一个文件\文件夹的属性(包含了文件大小\最后修改时间) 34// [[NSFileManager defaultManager] attributesOfItemAtPath: error:<#(NSError *__autoreleasing *)#>]; 35 36 NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/resources/video.zip"]; 37// 默认就是GET请求 38 NSURLRequest *request = [NSURLRequest requestWithURL:url]; 39 [NSURLConnection connectionWithRequest:request delegate:self]; 40} 41 42#pragma mark - NSURLConnectionDataDelegate 代理方法 43/** 44 * 1. 当接受到服务器的响应(连通了服务器)就会调用 45*/ 46 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 47{ 48// 获得服务器的响应头 49// NSHTTPURLResponse *httpReponse = (NSHTTPURLResponse *)response; 50// NSLog(@"%@", httpReponse.allHeaderFields); 51 52// 0.文件的存储路径 53 NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 54 NSString *filepath = [caches stringByAppendingPathComponent:@"video.zip"]; 55 56// 1.创建一个空的文件到沙盒中 57 NSFileManager *mgr = [NSFileManager defaultManager]; 58// 刚创建完毕的大小是0字节 59 [mgr createFileAtPath:filepath contents:nil attributes:nil]; 60 61// 2.创建写数据的文件句柄 62 self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filepath]; 63 64// 3.获得完整文件的长度 65 self.totalLength = response.expectedContentLength; 66} 67 68/** 69 * 2. 当接受到服务器的数据就会调用(可能会被调用多次, 每次调用只会传递部分数据) 70*/ 71 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 72{ 73// 累加长度 74 self.currentLength += data.length; 75 76// 显示进度 77double progress = (double)self.currentLength / self.totalLength; 78 self.progressView.progress = progress; 79 80// 移动到文件的尾部 81 [self.writeHandle seekToEndOfFile]; 82// 从当前移动的位置(文件尾部)开始写入数据 83 [self.writeHandle writeData:data]; 84} 85 86/** 87 * 3. 当服务器的数据接受完毕后就会调用 88*/ 89 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 90{ 91 NSLog(@"connectionDidFinishLoading----"); 92 93// 清空属性值 94 self.currentLength = 0; 95 self.totalLength = 0; 96 97// 关闭连接(不再输入数据到文件中) 98 [self.writeHandle closeFile]; 99 self.writeHandle = nil; 100} 101102/** 103 * 请求错误(失败)的时候调用(请求超时\断网\没有网, 一般指客户端错误) 104*/105 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 106{ 107108} 109 @end
原文:http://www.cnblogs.com/PJHome/p/5158347.html
内容总结
以上是互联网集市为您收集整理的ios大文件下载全部内容,希望文章能够帮你解决ios大文件下载所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。