IOS上传文件开发
QQ群:74432915 欢迎大家一起探讨
#import <UIKit/UIKit.h>
#import "AFHTTPRequestOperationManager.h"
@interface FKAppDelegate :UIResponder <UIApplicationDelegate>
@property (strong,nonatomic)UIWindow *window;
@property (strong,nonatomic)AFHTTPRequestOperationManager* manager;
@end
然后在 FKAppDelegate.m文件初始化
#import "FKAppDelegate.h"
@implementation FKAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.manager = [AFHTTPRequestOperationManagermanager];
self.manager.responseSerializer = [[AFHTTPResponseSerializeralloc]init];
return YES;
}
然后在自定义ViewController主要做例如以下操作
#import "FKViewController.h"
#import "FKAppDelegate.h"
@interface FKViewController ()
{
FKAppDelegate* appDelegate;
NSArray* images;
}
@end
@implementation FKViewController
- (void)viewDidLoad
{
[superviewDidLoad];
appDelegate = [UIApplicationsharedApplication].delegate;
self.picker.dataSource = self;
self.picker.delegate = self;
// 使用简化语法创建NSArray集合
images =@[@"logo",@"java" , @"android"];
}
// UIPickerViewDataSource中定义的方法。该方法返回值决定该控件包括多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
//返回1表明该控件仅仅包括1列
return1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
returnimages.count;
}
#define kImageTag 1
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:
(NSInteger)row forComponent:(NSInteger)component
reusingView:(UIView *)view
{
//假设可重用的view的tag不等于kImageTag,表明该view已经不存在,须要又一次创建
if(view.tag !=kImageTag)
{
view = [[UIViewalloc]init];
// 为该UIView设置tag属性
view.tag =kImageTag;
//设置不同意用户交互
view.userInteractionEnabled =NO;
UIImageView* iv = [[UIImageViewalloc]initWithImage:
[UIImageimageNamed:[imagesobjectAtIndex:row]]];
iv.frame =CGRectMake(0 ,0 , 48 ,48);
iv.contentMode =UIViewContentModeScaleAspectFit;
[viewaddSubview:iv];
}
return view;
}
// UIPickerViewDelegate中定义的方法,该方法的返回值决定列表项的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView
rowHeightForComponent:(NSInteger)component
{
return48;
}
// UIPickerViewDelegate中定义的方法,该方法的返回值决定列表项的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView
widthForComponent:(NSInteger)component
{
return48;
}
- (IBAction)upload:(id)sender
{
//获取用户选中的行
NSInteger selectedRow = [self.pickerselectedRowInComponent:0];
//获取用户选中的文件名称
NSString* fileName = [imagesobjectAtIndex:selectedRow];
//依据用户选中的文件名称确定须要上传的文件
NSURL *filePath = [[NSBundlemainBundle]URLForResource:fileName
withExtension:@"png"];
NSDictionary *parameters =@{@"name":@"额外的请求參数"};
// 使用AFHTTPRequestOperationManager发送POST请求
[appDelegate.manager
POST:@"http://192.168.1.88:8888/AFNetworkingServer/upload"
parameters:parameters
//使用代码块来封装要上传的文件数据
constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
[formDataappendPartWithFileURL:filePath //指定上传的文件
name:@"file" //指定上传文件相应的请求參数名
//指定上传文件的原始文件名称
fileName:[NSStringstringWithFormat:@"%@.png" ,fileName]
//指定上传文件的MIME类型
mimeType:@"image/png"
error:nil];
}
//获取server响应成功时激发的代码块
success:^(AFHTTPRequestOperation *operation,id responseObject)
{
//当使用HTTP响应解析器时,server响应数据被封装在NSData中
// 此处将NSData转换成NSString、并使用UIAlertView显示登录结果
[[[UIAlertViewalloc]initWithTitle:@"上传结果" message:
[[NSStringalloc]initWithData:responseObjectencoding:
NSUTF8StringEncoding]delegate:self
cancelButtonTitle:@"确定"otherButtonTitles:nil]
show];
}
//获取server响应失败时激发的代码块
failure:^(AFHTTPRequestOperation *operation,NSError *error)
{
NSLog(@"获取server响应出错!
");
}];
}
@end
注:本文章属于个人原创 请尊重个人劳动成果,谢谢。
版权声明:本文博主原创文章,博客,未经同意,不得转载。
本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4758607.html,如需转载请自行联系原作者