iOS用ASIHttpRequest上傳

來源:互聯網
上載者:User

1.建立一個single view工程,匯入ASIHttpRequest庫,匯入MobileCoreServices、CFNetwork、SystemConfiguration和libz1.2.5.dylib四個系統庫

2.隨便匯入一張圖片,比如haoyou.png

3.ViewController.h

#import #import "ASIHTTPRequest.h"#import "ASIFormDataRequest.h"@interface ViewController : UIViewController @property (nonatomic, copy)NSString *m_auth;@end

4.ViewController.m 添加兩個按鈕

- (void)viewDidLoad {    [super viewDidLoad];    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];    loginBtn.frame = CGRectMake(100, 20, 120, 40);    [loginBtn setTitle:@"登入" forState:UIControlStateNormal];    [loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:loginBtn];        UIButton *uploadBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];    uploadBtn.frame = CGRectMake(100, 80, 120, 40);    [uploadBtn setTitle:@"上傳" forState:UIControlStateNormal];    [uploadBtn   addTarget:self action:@selector(upload) forControlEvents:UIControlEventTouchUpInside];     [self.view addSubview:uploadBtn];}

5.實現login和upload兩個方法

- (void)login {    NSURL *url = [NSURL URLWithString:@"..."];//此處省略請求url    //請求    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];    request.tag = 10;    request.delegate = self;    [request startAsynchronous];}- (void)upload {    NSURL* url = [NSURL URLWithString:@"..."];//此處省略請求url    UIImage* img = [UIImage imageNamed:@"haoyou.png"];    NSData* data = UIImagePNGRepresentation(img);    //ASIFormDataRequest請求是post請求,可以查看其源碼    ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:url];    request.tag = 20;    request.delegate = self;    [request setPostValue:self.m_auth forKey:@"m_auth"];//    [request setFile:@"tabbar.png" forKey:@"haoyou"];//如果有路徑,上傳檔案    [request setData:data  withFileName:@"tmp.png" andContentType:@"image/png" forKey:@"headimage"];//               資料                檔案名稱,隨便起                 檔案類型            設定key    [request startAsynchronous];}

6.實現協議
- (void)requestFailed:(ASIHTTPRequest *)request {    NSLog(@"請求失敗");}- (void)requestFinished:(ASIHTTPRequest *)request {    if (request.tag == 10) {        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:request.responseData options:0 error:nil];        self.m_auth = [dic objectForKey:@"m_auth"];        NSLog(@"%@", self.m_auth);    }    if (request.tag == 20) {        NSLog(@"%@", request.responseString);    }}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.