ios23- 檔案下載(同步和非同步)

來源:互聯網
上載者:User

1.第一步:建立一個單例視圖


#import <UIKit/UIKit.h>

 

@interface ios23_downViewController : UIViewController<NSURLConnectionDelegate,NSURLConnectionDataDelegate>{

    NSMutableData *connectionData;

   
}

-(IBAction)tongbu;

-(IBAction)yibu;

@property (nonatomic,retain) NSMutableData *connectionData;

@end

2
//

//  ios23_downViewController.m

//  ios23-down

//

//  Created by  on 13-6-17.

//  Copyright 2013年 __MyCompanyName__. All rights reserved.

//

 

#import "ios23_downViewController.h"

 

@implementation ios23_downViewController

@synthesize connectionData;

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.

}

 

#pragma mark - View lifecycle

-(IBAction)tongbu{

    NSLog(@"同步");

    NSError *err;

    //定義url

    NSString *url=@"http://172.22.65.38/new/1.doc";

    //構建NSURL

    NSURL *fileUrl=[NSURL URLWithString:url];

    //構建nsurlrequest

    NSURLRequest *request=[[NSURLRequest alloc]initWithURL:fileUrl];

    //建立串連

    NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&err];

    if (data.length>0) {

        NSString *savePath=[[NSHomeDirectory()stringByAppendingPathComponent:@"documents"]stringByAppendingPathComponent:@"test.zip"];

        //當資料寫入的時候

        if ([data writeToFile:savePath atomically:YES]) {

            NSLog(@"儲存成功");

        }else{

            NSLog(@"儲存失敗");

        }

    }

               
          
}

-(IBAction)yibu{

    NSLog(@"非同步");

    NSError *err;

    //定義url

    NSString *url=@"http://172.22.65.38/new/1.doc";

    //構建NSURL

    NSURL *fileUrl=[NSURL URLWithString:url];

    //構建nsurlrequest

    NSURLRequest *request=[[NSURLRequest alloc]initWithURL:fileUrl];

    //建立串連

    NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&err];

    NSURLConnection *conn=[[NSURLConnection alloc]initWithRequest:request delegate:self];

    //初始化connectionData;

    connectionData=[[NSMutableData alloc]init ];

   
}

//接受資料

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    //擷取伺服器傳遞的資料

    [connectionData appendData:data];

}

//接收資料成功

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

    if (connectionData.length>0) {

        NSString *savePath=[[NSHomeDirectory()stringByAppendingPathComponent:@"documents"]stringByAppendingPathComponent:@"test.zip"];

        //當資料寫入的時候

        if ([connectionData writeToFile:savePath atomically:YES]) {

            NSLog(@"儲存成功");

        }else{

            NSLog(@"儲存失敗");

        }

    }}

//接收資料失敗

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{}

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

 

- (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}

 

- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

}

 

- (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

}

 

- (void)viewWillDisappear:(BOOL)animated

{

[super viewWillDisappear:animated];

}

 

- (void)viewDidDisappear:(BOOL)animated

{

[super viewDidDisappear:animated];

}

 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}

 

@end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.