如何简单的下载一些小文件并保存在一个路径中(有些方法进行了封装)
来源:互联网 发布:js获取div id值 编辑:程序博客网 时间:2024/05/22 17:27
#import "HomeViewController.h"@interface HomeViewController () <NSURLConnectionDataDelegate>{ UITextField *field; NSFileHandle *handleFile; UIProgressView *progressView; long long totalLength; NSURLConnection *connect;}@end@implementation HomeViewController- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void) creatProgress{ progressView = [[UIProgressView alloc] init]; progressView.frame = CGRectMake(20, 300, 250, 20); progressView.progress = 0; [self.view addSubview:progressView];}//封装progress//- (UIProgressView *) progressView : (CGRect)fram andValue :(int)ValueProgress//{// progressView = [[UIProgressView alloc] init];// progressView.frame = fram;// progressView.progress = ValueProgress;// [self.view addSubview:progressView];// return progressView;//}- (void) creatTextField{ field = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 280, 30)]; field.backgroundColor = [UIColor blackColor]; field.textColor = [UIColor redColor]; [self.view addSubview:field];}//封装textField//- (UITextField *) field :(CGRect)fram andBackgroundColor :(UIColor *)backColor andTextColor: (UIColor *)textColor//{// field = [[UITextField alloc] initWithFrame:fram];// field.backgroundColor = backColor;// field.textColor = textColor;// [self.view addSubview:field];// return field//}- (void) creatDownBtn{ UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.backgroundColor = [UIColor yellowColor]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted]; [btn setTitle:@"下载" forState:UIControlStateNormal]; btn.frame = CGRectMake(30, 200, 100, 30); [btn addTarget:self action:@selector(downLoad) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; }//- (UIButton *) creatBtn :(UIColor *)backgroundColor andName :(NSString *)name andFram :(CGRect)fram andAction :(SEL)action//{// UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];// btn.backgroundColor = backgroundColor;// [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];// [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];// [btn setTitle:name forState:UIControlStateNormal];// btn.frame = fram;// [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];// [self.view addSubview:btn];// return btn;//}- (void)viewDidLoad{ [super viewDidLoad]; [self creatTextField]; [self creatDownBtn]; [self creatProgress]; }- (void) downLoad{ NSFileManager *fileManager = [NSFileManager defaultManager]; NSURL *url = [NSURL URLWithString:field.text]; NSString *path0 = @"/Users/gaozhenyusky/Desktop/下载测试/test"; NSString *path = [path0 stringByAppendingPathExtension:[url pathExtension]]; [fileManager createFileAtPath:path contents:nil attributes:nil]; handleFile = [NSFileHandle fileHandleForWritingAtPath:path]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; connect = [NSURLConnection connectionWithRequest:request delegate:self]; [connect start];}- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ NSLog(@"%@",response); totalLength = response.expectedContentLength;}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [handleFile writeData:data]; float progressValue = (float)[handleFile offsetInFile]/totalLength; progressView.progress = progressValue; NSLog(@"%f",progressValue);}- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"出错了!!!"); }- (void) connectionDidFinishLoading:(NSURLConnection *)connection{ [handleFile closeFile];}
0 0
- 如何简单的下载一些小文件并保存在一个路径中(有些方法进行了封装)
- 下载一个网站的html并保存到文件中
- 对下载进行一个简单的封装
- 小秀:如何在获得了文件路径以后获得文件名称的方法!
- 用java语言编译内存中java小程序,并将生成的.class文件保存在一个文件夹下。
- c#读取文件路径并保存在textBox2中
- 取得一个文件夹下的文件,并保存在一个文件中.
- 多线程之线程间的通信——以及下载文件并保存到指定路径的方法
- 简单的C# 保存内容到指定路径下的XML文件中方法
- 自己封装了一个socket下载文件的类
- android项目中如何获取assets下面的文件在SD卡上面的保存路径
- 在linux中进行文件下载并解压
- MVC 保存二进制到数据库,并显示文件的方法(图片显示或下载文件)
- 如何在 Linux 中恢复一个删除了的文件
- 在VC中如何使用对话框让用户选择自己保存文件的路径
- 在VC中如何使用对话框让用户选择自己保存文件的路径
- 如何从一个文件特定段落中(文本文件),读取相关的文件信息,并进行分段梳理
- java 导出excel封装了一个简单的导出方法。
- SpringMVC+Hibernate+Spring框架之一(Hibernate 分页实现,视图采用Freemarker)
- flex按照字段排序
- 继承知识点总结
- Java异常体系结构
- 逆序三位数
- 如何简单的下载一些小文件并保存在一个路径中(有些方法进行了封装)
- MATLAB中求图像中某一区域的质心
- flex导出excel的注意事项
- Oracle 11G常见错误解决
- C++常用库
- 网络红人程琳被吸奶
- LeetCode Populating Next Right Pointers in Each Node
- Flex 4 中有个BUG textInput textArea不能输入中文(转)
- bzoj1483 [HNOI2009]梦幻布丁