[iOS 多线程 & 网络 - 2.11] - ASI框架上传文件

A.ASI的上传功能基本使用
1.实现步骤
(1)创建请求
使用ASIFormDataRequest
(2)设置上传文件路径
(3)发送请求
 
 
2.上传相册相片
UIImagePickerController用来选择图片
设置图片来源,可以选择相册
使用代理 UIImagePickerControllerDelegate方法,选择完成之后取得相片
 
  1 //

  2 //  ViewController.m

  3 //  ASIUploadDemo

  4 //

  5 //  Created by hellovoidworld on 15/1/28.

  6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.

  7 //

  8 

  9 #import "ViewController.h"

 10 #import "ASIFormDataRequest.h"

 11 

 12 @interface ViewController () <ASIProgressDelegate, UINavigationControllerDelegate,UIImagePickerControllerDelegate>

 13 

 14 @property (weak, nonatomic) IBOutlet UIProgressView *progressView;

 15 

 16 @property(nonatomic, strong) ASIFormDataRequest *request;

 17 

 18 - (IBAction)startUploading;

 19 

 20 @end

 21 

 22 @implementation ViewController

 23 

 24 - (void)viewDidLoad {

 25     [super viewDidLoad];

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

 27    

 28     // 显示网络请求状态

 29     [ASIHTTPRequest setShouldThrottleBandwidthForWWAN:YES];

 30 }

 31 

 32 /** 回收 */

 33 - (void)dealloc {

 34     [self.request clearDelegatesAndCancel];

 35    

 36 //    self.request = nil;

 37 }

 38 

 39 #pragma mark - 上传方法

 40 - (IBAction)startUploading {

 41 //    [self upload];

 42 //    [self upload2];

 43    

 44     [self uploadImageFromAlbum];

 45 }

 46 

 47 #pragma mark - 上传文件

 48 

 49 /** 上传方法1 */

 50 - (void) upload {

 51     // 1.创建请求

 52     NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/upload"];

 53     self.request = [ASIFormDataRequest requestWithURL:url];

 54    

 55     // 2.设置上传文件

 56     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"bird.jpg" ofType:nil];

 57     [self.request setFile:filePath forKey:@"uploadedFile"];

 58    

 59     // 3.设置其他参数

 60     [self.request setPostValue:@"tom" forKey:@"user"];

 61 

 62     // 进度条

 63     self.request.uploadProgressDelegate = self.progressView;

 64    

 65     // 程序进入后台,仍然继续发送请求,适合发送大文件时使用

 66     self.request.shouldContinueWhenAppEntersBackground = YES;

 67    

 68     // 4.发送请求

 69     [self.request startAsynchronous];

 70    

 71     // 5.监听请求

 72     self.request.completionBlock = ^{

 73         NSLog(@"上传完成!");

 74     };

 75 }

 76 

 77 

 78 /** 上传方法2,从新设置上传文件名和类型 */

 79 - (void) upload2 {

 80     // 1.创建请求

 81     NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/upload"];

 82     self.request = [ASIFormDataRequest requestWithURL:url];

 83    

 84     // 2.设置上传文件

 85     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"bird.jpg" ofType:nil];

 86     // 从新设置上传文件名和类型

 87     [self.request setFile:filePath withFileName:@"my_file.jpg" andContentType:@"image/jpg" forKey:@"uploadedFile"];

 88    

 89     // 3.设置其他参数

 90     [self.request setPostValue:@"tom" forKey:@"user"];

 91    

 92     // 进度条

 93     self.request.uploadProgressDelegate = self.progressView;

 94    

 95     // 程序进入后台,仍然继续发送请求,适合发送大文件时使用

 96     self.request.shouldContinueWhenAppEntersBackground = YES;

 97    

 98     // 4.发送请求

 99     [self.request startAsynchronous];

100    

101     // 5.监听请求

102     self.request.completionBlock = ^{

103         NSLog(@"上传完成!");

104     };

105 }

106 

107 #pragma mark - 从相册/相机选择照片上传

108 - (void) uploadImageFromAlbum {

109     // 创建一个相片选择控制器

110     UIImagePickerController *imagePC = [[UIImagePickerController alloc] init];

111    

112     // 设置来源

113 //    [imagePC setSourceType:UIImagePickerControllerSourceTypeCamera]; // 相机

114     [imagePC setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; // 相簿

115    

116     // 设置代理

117     imagePC.delegate = self;

118    

119     // 弹出相片控制器

120     [self presentViewController:imagePC animated:YES completion:^{

121         NSLog(@"开始选择相片");

122     }];

123 }

124 

125 - (void) uploadImage:(UIImage *) image {

126     // 1.创建请求

127     NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/upload"];

128     self.request = [ASIFormDataRequest requestWithURL:url];

129    

130     // 2.将上传图片转换成二进制数据

131     NSData *imageData = UIImagePNGRepresentation(image);

132    

133     // 从新设置上传文件名和类型

134     [self.request setData:imageData withFileName:@"my_photo.png" andContentType:@"iamge/png" forKey:@"uploadedFile"];

135    

136     // 3.设置其他参数

137     [self.request setPostValue:@"tom" forKey:@"user"];

138    

139     // 进度条

140     self.request.uploadProgressDelegate = self.progressView;

141    

142     // 程序进入后台,仍然继续发送请求,适合发送大文件时使用

143     self.request.shouldContinueWhenAppEntersBackground = YES;

144    

145     // 4.发送请求

146     [self.request startAsynchronous];

147    

148     // 5.监听请求

149     self.request.completionBlock = ^{

150         NSLog(@"上传完成!");

151     };

152 }

153 

154 #pragma mark - UIImagePickerControllerDelegate

155 /** 完成选择照片代理方法 */

156 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

157    

158     // 退出相片选择控制器

159     [picker dismissViewControllerAnimated:YES completion:^{

160         NSLog(@"已经选择相片,准备上传");

161     }];

162    

163     // 得到上传图片

164     UIImage *image = info[UIImagePickerControllerOriginalImage];

165    

166     // 上传图片

167     [self uploadImage:image];

168 }

169 

170 @end

 

 
 
 

你可能感兴趣的:(上传文件)