把下载的文件存到指定的文件夹中
2013-11-06 21:37
183 查看
在.h 文件中 实现NSURLConnectionDataDelegate协议
@property (nonatomic, assign) IBOutlet UIProgressView *progressView;
- (IBAction)download;
{
// 文件的总长度
int _totalLength;
}
@property (nonatomic, retain) NSMutableData *data;
.m 文件中
- (void)dealloc {
[_data release];
[super dealloc];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.data = nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)download {
self.data = [NSMutableData data];
NSURL *url = [NSURL URLWithString:@"http://192.168.1.106:8080/MJServer/movie.avi"];
NSURLRequest *reuqest = [NSURLRequest requestWithURL:url];
[[NSURLConnection connectionWithRequest:reuqest delegate:self] start];
}
#pragma mark - 代理方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.data appendData:data];
// 设置下载进度
self.progressView.progress = (float)self.data.length / (float)_totalLength;
}
#pragma mark 服务器有相应就会调用这个方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSHTTPURLResponse *rep = (NSHTTPURLResponse *)response;
_totalLength = [[[rep allHeaderFields] objectForKey:@"Content-Length"] intValue];
}
//下载完成后放到指定的文件夹中
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *dir = [cacheDir stringByAppendingPathComponent:@"movie"];
NSFileManager *mgr = [NSFileManager defaultManager];
// 是否为文件夹
BOOL isDir;
// 是否存在
BOOL exist = [mgr fileExistsAtPath:dir isDirectory:&isDir];
if (!exist) {
// 创建一个新的文件夹
[mgr createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *path = [dir stringByAppendingPathComponent:@"mj.avi"];
// 将文件数据写入Library/Caches文件夹下
[self.data writeToFile:path atomically:YES];
}
@property (nonatomic, assign) IBOutlet UIProgressView *progressView;
- (IBAction)download;
{
// 文件的总长度
int _totalLength;
}
@property (nonatomic, retain) NSMutableData *data;
.m 文件中
- (void)dealloc {
[_data release];
[super dealloc];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.data = nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)download {
self.data = [NSMutableData data];
NSURL *url = [NSURL URLWithString:@"http://192.168.1.106:8080/MJServer/movie.avi"];
NSURLRequest *reuqest = [NSURLRequest requestWithURL:url];
[[NSURLConnection connectionWithRequest:reuqest delegate:self] start];
}
#pragma mark - 代理方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.data appendData:data];
// 设置下载进度
self.progressView.progress = (float)self.data.length / (float)_totalLength;
}
#pragma mark 服务器有相应就会调用这个方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSHTTPURLResponse *rep = (NSHTTPURLResponse *)response;
_totalLength = [[[rep allHeaderFields] objectForKey:@"Content-Length"] intValue];
}
//下载完成后放到指定的文件夹中
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *dir = [cacheDir stringByAppendingPathComponent:@"movie"];
NSFileManager *mgr = [NSFileManager defaultManager];
// 是否为文件夹
BOOL isDir;
// 是否存在
BOOL exist = [mgr fileExistsAtPath:dir isDirectory:&isDir];
if (!exist) {
// 创建一个新的文件夹
[mgr createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *path = [dir stringByAppendingPathComponent:@"mj.avi"];
// 将文件数据写入Library/Caches文件夹下
[self.data writeToFile:path atomically:YES];
}
相关文章推荐
- 生成的Excel出力到指定文件夹生成Zip文件再打包这些文件到页面下载
- C#打开指定文件夹及下载文件代码示例
- 将指定文件夹下的所有文件打包下载
- 下载文件到指定文件夹
- Linux 下 在 Github 上下载指定文件夹或者文件
- php实现将远程文件下载保存到服务器指定文件夹
- 判断是否下载指定客户端(遍历硬盘上的全部文件文件夹)
- php下载文件并保存到指定文件夹
- 如何将服务器端文件 自动下载并保存到客户端IE临时文件夹 或者指定文件夹
- asp.net从服务器(指定文件夹)下载任意格式的文件到本地
- 基于Java语言完成:使用URL下载指定的文件保存到指定的文件夹中
- c#下载网上文件到指定文件夹
- 需求(Java语言实现):对存有图片URL的txt文件进行逐行读取,自行下载txt文件中的图片到指定文件夹中
- Asp.net 获取服务器指定文件夹目录文件,并提供下载
- Asp.net获取服务器指定文件夹目录文件并提供下载的方法
- Android okhttp 下载文件到指定文件夹带进度更新加7.0适配
- 显示指定文件夹中的所有文件并提供下载[WEB学习实例]
- 使用URL下载指定的文件保存到指定的文件夹中。
- Asp.net获取服务器指定文件夹目录文件并提供下载的方法
- 移动指定文件夹内的全部文件