IOS文件下载

举报
清雨小竹 发表于 2022/09/25 00:38:09 2022/09/25
【摘要】 iOS开发中会经常用到文件上传下载的功能,这篇文件将介绍一下如何结合asp.net webservice实现文件上传下载。  首先,让我们看下文件下载。 这里我们下载cnblogs上的一个zip文件。使用NSURLRequest+NSURLConnection可以很方便的实现这个功能。在asp.net webserv...

iOS开发中会经常用到文件上传下载的功能,这篇文件将介绍一下如何结合asp.net webservice实现文件上传下载。 
首先,让我们看下文件下载。

这里我们下载cnblogs上的一个zip文件。使用NSURLRequest+NSURLConnection可以很方便的实现这个功能。在asp.net webservice中可以将文件的地址返回到iOS系统,iOS系统再通过这个url去请求下载该文件。这里为了简单起见,直接将url写道代码里面了。我们可以使用两种方式去下载文件。

1、同步下载文件:


      NSString *urlAsString = @"http://files.cnblogs.com/zhuqil/UIWebViewDemo.zip";
              NSURL    *url = [NSURL URLWithString:urlAsString];
              NSURLRequest *request = [NSURLRequest requestWithURL:url];
              NSError *error = nil;
              NSData   *data = http://www.cnblogs.com/zhwl/archive/2012/07/13/[NSURLConnection sendSynchronousRequest:request 
                                                     returningResponse:nil
                                                                 error:&error];
             /* 下载的数据 */
             if (data != nil){
                 NSLog(@"下载成功");
                 if ([data writeToFile:@"UIWebViewDemo.zip" atomically:YES]) {
                     NSLog(@"保存成功.");
                  }
                 else
                  {
                     NSLog(@"保存失败.");
                  }
              } else {
                 NSLog(@"%@", error);
              }
  
 


2.异步下载


      DownLoadingViewController.h
      // DownLoadingViewController.h 
      // DownLoading 
      // 
      // Created by skylin zhu on 11-7-30. 
      // Copyright 2011年 mysoft. All rights reserved. 
      // 
      #import 
      @interface DownLoadingViewController : UIViewController {
          NSURLConnection *connection;
          NSMutableData *connectionData;
      }
      @property (nonatomic,retain) NSURLConnection *connection;
      @property (nonatomic,retain) NSMutableData *connectionData;
      @end
      DownLoadingViewController.m
      // DownLoadingViewController.m 
      // DownLoading 
      // 
      // Created by skylin zhu on 11-7-30. 
      // Copyright 2011年 mysoft. All rights reserved. 
      // 
      #import "DownLoadingViewController.h" 
      @implementation DownLoadingViewController
      @synthesize connection,connectionData;
      - (void)dealloc
      {
          [super dealloc];
      }
      - (void)didReceiveMemoryWarning
      {
         // Releases the view if it doesn't have a superview. 
          [super didReceiveMemoryWarning];
         // Release any cached data, images, etc that aren't in use. 
      }
      #pragma mark - View lifecycle 
      // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
      - (void)viewDidLoad
      {
          [super viewDidLoad];
         //文件地址 
          NSString *urlAsString = @"http://files.cnblogs.com/zhuqil/UIWebViewDemo.zip";
          NSURL    *url = [NSURL URLWithString:urlAsString];
          NSURLRequest *request = [NSURLRequest requestWithURL:url];
          NSMutableData *data = http://www.cnblogs.com/zhwl/archive/2012/07/13/[[NSMutableData alloc] init]; 
          self.connectionData = http://www.cnblogs.com/zhwl/archive/2012/07/13/data; 
          [data release];
          NSURLConnection *newConnection = [[NSURLConnection alloc]
                                            initWithRequest:request
                                            delegate:self
                                            startImmediately:YES];
          self.connection = newConnection;
          [newConnection release];
         if (self.connection != nil){
            NSLog(@"Successfully created the connection");
          } else {
             NSLog(@"Could not create the connection");
          }
      }
      - (void) connection:(NSURLConnection *)connection
                  didFailWithError:(NSError *)error{
         NSLog(@"An error happened");
         NSLog(@"%@", error);
      }
      - (void) connection:(NSURLConnection *)connection
                    didReceiveData:(NSData *)data{
         NSLog(@"Received data");
          [self.connectionData appendData:data];
      }
      - (void) connectionDidFinishLoading
      :(NSURLConnection *)connection{
         /* 下载的数据 */
             NSLog(@"下载成功");
             if ([self.connectionData writeToFile:@"UIWebViewDemo.zip" atomically:YES]) {
                 NSLog(@"保存成功.");
              }
             else
              {
                 NSLog(@"保存失败.");
              }
         /* do something with the data here */
      }
      - (void) connection:(NSURLConnection *)connection
                didReceiveResponse:(NSURLResponse *)response{
          [self.connectionData setLength:0];
      }
      - (void) viewDidUnload{
          [super viewDidUnload];
          [self.connection cancel];
          self.connection = nil;
          self.connectionData = http://www.cnblogs.com/zhwl/archive/2012/07/13/nil; 
      }
      @end
  
 

文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。

原文链接:zzzili.blog.csdn.net/article/details/8424164

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。