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

A.ASI的上传功能基本使用
1.实现步骤
(1)创建请求
使用ASIFormDataRequest
(2)设置上传文件路径
(3)发送请求
 
 
2.上传相册相片
UIImagePickerController用来选择图片
设置图片来源,可以选择相册
使用代理 UIImagePickerControllerDelegate方法,选择完成之后取得相片
 
 //
// ViewController.m
// ASIUploadDemo
//
// Created by hellovoidworld on 15/1/28.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "ViewController.h"
#import "ASIFormDataRequest.h" @interface ViewController () <ASIProgressDelegate, UINavigationControllerDelegate,UIImagePickerControllerDelegate> @property (weak, nonatomic) IBOutlet UIProgressView *progressView; @property(nonatomic, strong) ASIFormDataRequest *request; - (IBAction)startUploading; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // 显示网络请求状态
[ASIHTTPRequest setShouldThrottleBandwidthForWWAN:YES];
} /** 回收 */
- (void)dealloc {
[self.request clearDelegatesAndCancel]; // self.request = nil;
} #pragma mark - 上传方法
- (IBAction)startUploading {
// [self upload];
// [self upload2]; [self uploadImageFromAlbum];
} #pragma mark - 上传文件 /** 上传方法1 */
- (void) upload {
// 1.创建请求
NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/upload"];
self.request = [ASIFormDataRequest requestWithURL:url]; // 2.设置上传文件
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"bird.jpg" ofType:nil];
[self.request setFile:filePath forKey:@"uploadedFile"]; // 3.设置其他参数
[self.request setPostValue:@"tom" forKey:@"user"]; // 进度条
self.request.uploadProgressDelegate = self.progressView; // 程序进入后台,仍然继续发送请求,适合发送大文件时使用
self.request.shouldContinueWhenAppEntersBackground = YES; // 4.发送请求
[self.request startAsynchronous]; // 5.监听请求
self.request.completionBlock = ^{
NSLog(@"上传完成!");
};
} /** 上传方法2,从新设置上传文件名和类型 */
- (void) upload2 {
// 1.创建请求
NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/upload"];
self.request = [ASIFormDataRequest requestWithURL:url]; // 2.设置上传文件
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"bird.jpg" ofType:nil];
// 从新设置上传文件名和类型
[self.request setFile:filePath withFileName:@"my_file.jpg" andContentType:@"image/jpg" forKey:@"uploadedFile"]; // 3.设置其他参数
[self.request setPostValue:@"tom" forKey:@"user"]; // 进度条
self.request.uploadProgressDelegate = self.progressView; // 程序进入后台,仍然继续发送请求,适合发送大文件时使用
self.request.shouldContinueWhenAppEntersBackground = YES; // 4.发送请求
[self.request startAsynchronous]; // 5.监听请求
self.request.completionBlock = ^{
NSLog(@"上传完成!");
};
} #pragma mark - 从相册/相机选择照片上传
- (void) uploadImageFromAlbum {
// 创建一个相片选择控制器
UIImagePickerController *imagePC = [[UIImagePickerController alloc] init]; // 设置来源
// [imagePC setSourceType:UIImagePickerControllerSourceTypeCamera]; // 相机
[imagePC setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; // 相簿 // 设置代理
imagePC.delegate = self; // 弹出相片控制器
[self presentViewController:imagePC animated:YES completion:^{
NSLog(@"开始选择相片");
}];
} - (void) uploadImage:(UIImage *) image {
// 1.创建请求
NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/upload"];
self.request = [ASIFormDataRequest requestWithURL:url]; // 2.将上传图片转换成二进制数据
NSData *imageData = UIImagePNGRepresentation(image); // 从新设置上传文件名和类型
[self.request setData:imageData withFileName:@"my_photo.png" andContentType:@"iamge/png" forKey:@"uploadedFile"]; // 3.设置其他参数
[self.request setPostValue:@"tom" forKey:@"user"]; // 进度条
self.request.uploadProgressDelegate = self.progressView; // 程序进入后台,仍然继续发送请求,适合发送大文件时使用
self.request.shouldContinueWhenAppEntersBackground = YES; // 4.发送请求
[self.request startAsynchronous]; // 5.监听请求
self.request.completionBlock = ^{
NSLog(@"上传完成!");
};
} #pragma mark - UIImagePickerControllerDelegate
/** 完成选择照片代理方法 */
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { // 退出相片选择控制器
[picker dismissViewControllerAnimated:YES completion:^{
NSLog(@"已经选择相片,准备上传");
}]; // 得到上传图片
UIImage *image = info[UIImagePickerControllerOriginalImage]; // 上传图片
[self uploadImage:image];
} @end
 
 
 
上一篇:Chrome 插件编写日记


下一篇:Java网络编程(TCP协议-练习-上传文本文件)