您的位置:首页 > 移动开发 > IOS开发

iOS-实现文件上传下载

2014-02-12 14:25 260 查看
OS开发中会经常用到文件上传下载的功能,这篇文件将介绍一下使用asp.net webservice实现文件上传下载。

首先,让我们看下文件下载。

这里我们下载cnblogs上的一个zip文件。使用NSURLRequest+NSURLConnection可以很方便的实现这个功能。

同步下载文件:

NSString *urlAsString = @"http://files.cnblogs.com/zhuqil/UIWebViewDemo.zip";

        NSURL    *url = [NSURL URLWithString:urlAsString];

        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        NSError *error = nil;

        NSData   *data = [NSURLConnection sendSynchronousRequest:request

                                               returningResponse:nil

                                                           error:&error];

        /* 下载的数据 */

        if (data != nil){

            NSLog(@"下载成功");

            if ([data writeToFile:@"UIWebViewDemo.zip" atomically:YES]) {

                NSLog(@"保存成功.");

            }

            else

            {

                NSLog(@"保存失败.");

            }

        } else {

            NSLog(@"%@", error);

        }

异步下载文件:

- (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 = [[NSMutableData alloc] init];

    self.connectionData = 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 = nil;

}

从上面两段代码中可以看到同步与异步下载的区别,大部分时候我们使用异步下载文件。在asp.net webservice中可以将文件的地址返回到iOS系统,iOS系统在去请求下载该文件。

上传文件

我们先使用VB.Net写一个webservice方法,用于接收上传上来的文件数据,代码如下。

文件上传接口
定义一个类PicOperation用于处理上传图片:

这里我主要定义了两个方法,一个是generateFormDataFromPostDictionary用于创建post form data,一个是UpLoading供调用的类上传图片,这个类需要一个UIimage的对象。
类定义好了,上传图片就非常方便了,看下面代码:

总结:这篇文章讲述了如何在iOS中结合asp.net webservice实现文件的上传和下载功能。

作者:朱祁林

出处:http://zhuqil.cnblogs.com 

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: