你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios23-文件上傳

ios23-文件上傳

編輯:IOS開發綜合

3.


//

//  ios23_uploadViewController.h

//  ios23-upload

//

//  Created by  on 13-6-17.

//  Copyright 2013年 __MyCompanyName__. All rights reserved.

//

 

#import <UIKit/UIKit.h>

#import "ASIHTTPRequest.h"

 

@interface ios23_uploadViewController : UIViewController<ASIHTTPRequestDelegate>

-(IBAction)upload;

 

@end

 

 

-------------------------------------------------


//

//  ios23_uploadViewController.m

//  ios23-upload

//

//  Created by  on 13-6-17.

//  Copyright 2013年 __MyCompanyName__. All rights reserved.

//

 

#import "ios23_uploadViewController.h"

#import "ASIHTTPRequest.h"

#import "ASIFormDataRequest.h"

 

@implementation ios23_uploadViewController

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

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

}

 

#pragma mark - View lifecycle

-(void)upload{

    //定義請求的URL地址:

    NSString *uploadURL = @"http://172.22.65.2/2012/upload.php";

    UIImage *im = [UIImage imageNamed:@"csdn"];//通過path圖片路徑獲取圖片

    NSData *data = UIImagePNGRepresentation(im);//獲取圖片數據

    /*

     ios中獲取圖片的方法有兩種,一種是UIImageJPEGRepresentation ,一種是UIImagePNGRepresentation前者獲取到圖片的數據量要比後者的小很多。。

     */

    NSURL *url = [NSURL URLWithString:uploadURL];

   
    ASIFormDataRequest *aRequest = [[ASIFormDataRequest alloc] initWithURL:url];

    [aRequest setDelegate:self];//代理

    [aRequest setRequestMethod:@"POST"];

    [aRequest addData:data withFileName:@"test.png" andContentType:@"image/png" forKey:@"file"];

    //forKey:@"file"  

    [aRequest addRequestHeader:@"Content-Type" value:@"binary/octet-stream"];//這裡的value值 需與服務器端 一致

   
    [aRequest startAsynchronous];//開始。異步

    [aRequest setDidFinishSelector:@selector(headPortraitSuccess)];//當成功後會自動觸發 headPortraitSuccess 方法

    [aRequest setDidFailSelector:@selector(headPortraitFail)];//如果失敗會 自動觸發 headPortraitFail 方法

  //  [aRequest release];

   


}

-(void)headPortraitSuccess{

   
    NSLog(@"上傳成功!");

   
}

 

-(void)headPortraitFail{

   
   
    NSLog(@"上傳失敗!");

   
}

 

//開始request請求

- (void)requestStarted:(ASIHTTPRequest *)request{

   
   
    NSLog(@"開始請求!");

   
}

 

- (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

 


 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved