在移動應用開發 文件形式上傳是必不可少的,最近把IOS這塊文件上傳文件代碼簡單的整理一下,如果大家有需要安卓這邊的代碼,本人也可以分享給大家!QQ群:74432915 歡迎大家一起探討
首先本demo采用網上開源框架 AFNetworking 源碼:http://download.csdn.net/detail/wangliang198901/7809439
將整個框架導入IOS新建立的工程中
在FKAppDelegate.h聲明 如下:
#import
#import "AFHTTPRequestOperationManager.h"
@interface FKAppDelegate :UIResponder
@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 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
源碼下載: http://download.csdn.net/detail/wangliang198901/7813361