iOS文件下載
iOS</span>開發中會經常用到文件上傳下載的功能,這篇文件將介紹一下如何結合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 </pre></span></span>