当前位置:首页>>ios23-文件上传

ios23-文件上传

  • 2022-08-05 23:49:05

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 = @"https://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

 


 

猜你喜欢