iOS-实现文件上传下载

来源:互联网 发布:淘宝卖的白酒是假酒吗 编辑:程序博客网 时间:2024/06/08 18:48
iOS开发中会经常用到文件上传下载的功能,这篇文件将介绍一下使用asp.net webservice实现文件上传下载。 
首先,让我们看下文件下载。 
这里我们下载cnblogs上的一个zip文件。使用NSURLRequest+NSURLConnection可以很方便的实现这个功能。 
同步下载文件: 

     
Java代码  收藏代码
  1. NSString *urlAsString = @"http://files.cnblogs.com/zhuqil/UIWebViewDemo.zip";  
  2.       NSURL    *url = [NSURL URLWithString:urlAsString];  
  3.       NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  4.       NSError *error = nil;  
  5.       NSData   *data = [NSURLConnection sendSynchronousRequest:request  
  6.                                              returningResponse:nil  
  7.                                                          error:&error];  
  8.       /* 下载的数据 */  
  9.       if (data != nil){  
  10.           NSLog(@"下载成功");  
  11.           if ([data writeToFile:@"UIWebViewDemo.zip" atomically:YES]) {  
  12.               NSLog(@"保存成功.");  
  13.           }  
  14.           else  
  15.           {  
  16.               NSLog(@"保存失败.");  
  17.           }  
  18.       } else {  
  19.           NSLog(@"%@", error);  
  20.       }   


异步下载文件: 

Java代码  收藏代码
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     //文件地址  
  5.     NSString *urlAsString = @"http://files.cnblogs.com/zhuqil/UIWebViewDemo.zip";  
  6.     NSURL    *url = [NSURL URLWithString:urlAsString];  
  7.     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  8.     NSMutableData *data = [[NSMutableData alloc] init];  
  9.     self.connectionData = data;  
  10.     [data release];  
  11.     NSURLConnection *newConnection = [[NSURLConnection alloc]  
  12.                                       initWithRequest:request  
  13.                                       delegate:self  
  14.                                       startImmediately:YES];  
  15.     self.connection = newConnection;  
  16.     [newConnection release];  
  17.     if (self.connection != nil){  
  18.        NSLog(@"Successfully created the connection");  
  19.     } else {  
  20.         NSLog(@"Could not create the connection");  
  21.     }  
  22. }  
  23.   
  24.   
  25.   
  26.   
  27. - (void) connection:(NSURLConnection *)connection  
  28.             didFailWithError:(NSError *)error{  
  29.     NSLog(@"An error happened");  
  30.     NSLog(@"%@", error);  
  31. }  
  32. - (void) connection:(NSURLConnection *)connection  
  33.               didReceiveData:(NSData *)data{  
  34.     NSLog(@"Received data");  
  35.     [self.connectionData appendData:data];  
  36. }  
  37. - (void) connectionDidFinishLoading  
  38. :(NSURLConnection *)connection{  
  39.     /* 下载的数据 */  
  40.   
  41.         NSLog(@"下载成功");  
  42.         if ([self.connectionData writeToFile:@"UIWebViewDemo.zip" atomically:YES]) {  
  43.             NSLog(@"保存成功.");  
  44.         }  
  45.         else  
  46.         {  
  47.             NSLog(@"保存失败.");  
  48.         }  
  49.     
  50.     /* do something with the data here */  
  51. }  
  52. - (void) connection:(NSURLConnection *)connection  
  53.           didReceiveResponse:(NSURLResponse *)response{  
  54.     [self.connectionData setLength:0];  
  55. }  
  56.   
  57. - (void) viewDidUnload{  
  58.     [super viewDidUnload];  
  59.     [self.connection cancel];  
  60.     self.connection = nil;  
  61.     self.connectionData = nil;  
  62. }  


从上面两段代码中可以看到同步与异步下载的区别,大部分时候我们使用异步下载文件。
0 0
原创粉丝点击