标签:ios 文件上传
#import <UIKit/UIKit.h>
#import "AFHTTPRequestOperationManager.h"
@interface FKAppDelegate :UIResponder <UIApplicationDelegate>
@property (strong,nonatomic)UIWindow *window;
@property (strong,nonatomic)AFHTTPRequestOperationManager* manager;
@end
#import "FKAppDelegate.h"
@implementation FKAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.manager = [AFHTTPRequestOperationManagermanager];
self.manager.responseSerializer = [[AFHTTPResponseSerializeralloc]init];
return YES;
}
#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];
}
//获取服务器响应成功时激发的代码块
success:^(AFHTTPRequestOperation *operation,id responseObject)
{
//当使用HTTP响应解析器时,服务器响应数据被封装在NSData中
// 此处将NSData转换成NSString、并使用UIAlertView显示登录结果
[[[UIAlertViewalloc]initWithTitle:@"上传结果" message:
[[NSStringalloc]initWithData:responseObjectencoding:
NSUTF8StringEncoding]delegate:self
cancelButtonTitle:@"确定"otherButtonTitles:nil]
show];
}
//获取服务器响应失败时激发的代码块
failure:^(AFHTTPRequestOperation *operation,NSError *error)
{
NSLog(@"获取服务器响应出错!");
}];
}
@end
标签:ios 文件上传
原文地址:http://blog.csdn.net/wangliang198901/article/details/38903845