现在的位置: 首页 > 综合 > 正文

IOS开发之文件上传

2018年03月31日 ⁄ 综合 ⁄ 共 3500字 ⁄ 字号 评论关闭

IOS开发之文件上传



    在移动应用开发  文件形式上传是必不可少的,最近把IOS这块文件上传文件代码简单的整理一下,如果大家有需要安卓这边的代码,本人也可以分享给大家!QQ群:74432915  欢迎大家一起探讨
首先本demo采用网上开源框架 AFNetworking  源码:http://download.csdn.net/detail/wangliang198901/7809439

将整个框架导入IOS新建立的工程中

FKAppDelegate.h声明 如下:
     

#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

{

//如果可重用的viewtag不等于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

源码下载: http://download.csdn.net/detail/wangliang198901/7813361

                   注:本文章属于个人原创  请尊重个人劳动成果,谢谢!




抱歉!评论已关闭.