#import "ViewController.h"
#import "CZNetworkTool.h"
#define kBounary @"bounary"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self demo3];
}
//NSURLSession 正确的上传方式
-(void)demo4{
// 1. 创建请求
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/httpLogin/post/upload-m.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 设置请求方法
request.HTTPMethod = @"POST";
// 设置请求头,告诉服务器本次长传的时文件信息 // 这一块的内容是不变的!
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",kBounary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
// 设置请求体
// 单文件上传
[[CZNetworkTool sharedNetworkTool] getHttpBodyWithFilePath:@"/Users/liaojianguo/Desktop/hehua.jpg" FileKey:@"userfile" FileName:@"荷花"];
// 多文件上传
// 多文件上传参数!
NSString *file1 = @"/Users/liaojianguo/Desktop/hehua.jpg";
NSString *name1 = @"荷花";
NSString *file2 = @"/Users/liaojianguo/Desktop/apple.png";
NSString *name2 = @"apple";
NSDictionary *fileDict = @{name1:file1,name2:file2};
NSString *key1 = @"username";
NSString *msg1 = @"zhangsan";
NSString *key2 = @"password";
NSString *msg2 = @"123456789";
NSDictionary *paramater = @{key1:msg1,key2:msg2};
NSData *data = [[CZNetworkTool sharedNetworkTool] getHttpBodyWithFileDict:fileDict fileKey:@"userfile[]" paramater:paramater];
// 只传递了文件参数
// request.HTTPBody = data;
// 2. 发送请求
// 普通网络任务一般不用来做上传或者下载!
// 可以用作上传/ 还可以用来做小文件的下载!
// 只能用 fromData 这个方法实现文件上传
// (NSData *) 就是封装好的二进制数据(请求体中的数据)
[[[NSURLSession sharedSession] uploadTaskWithRequest:request fromData:data completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//
NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}] resume];
}
// NSURLSession"错误"的上传方式 --能够实现上传 -- 可以使用
-(void)demo3
{
// 1. 创建请求
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/httpLogin/post/upload-m.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 设置请求方法
request.HTTPMethod = @"POST";
// 设置请求头,告诉服务器本次长传的时文件信息 // 这一块的内容是不变的!
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",kBounary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
// 设置请求体
// 单文件上传
[[CZNetworkTool sharedNetworkTool] getHttpBodyWithFilePath:@"/Users/liaojianguo/Desktop/hehua.jpg" FileKey:@"userfile" FileName:@"荷花"];
// 多文件上传
// 多文件上传参数!
NSString *file1 =@"/Users/liaojianguo/Desktop/hehua.jpg";
NSString *name1 = @"荷花";
NSString *file2 = @"/Users/liaojianguo/Desktop/apple.png";
NSString *name2 = @"apple";
NSDictionary *fileDict = @{name1:file1,name2:file2};
NSString *key1 = @"username";
NSString *msg1 = @"zhangsan";
NSString *key2 = @"password";
NSString *msg2 = @"123456789";
NSDictionary *paramater = @{key1:msg1,key2:msg2};
// 只传递了文件参数
request.HTTPBody = [[CZNetworkTool sharedNetworkTool] getHttpBodyWithFileDict:fileDict fileKey:@"userfile[]" paramater:paramater];
// 2. 发送请求
// 普通网络任务一般不用来做上传或者下载!
// 可以用作上传/ 还可以用来做小文件的下载!
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//
NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}] resume];
}
//(多文件+文本信息)POST上传
-(void)demo2{
// 上传参数(文件参数 + 普通文本参数)
NSString *file1 =@"/Users/liaojianguo/Desktop/hehua.jpg";
NSString *name1 = @"荷花";
NSString *file2 = @"/Users/liaojianguo/Desktop/apple.png";
NSString *name2 = @"apple";
NSDictionary *fileDict = @{name1:file1,name2:file2};
NSString *key1 = @"username";
NSString *msg1 = @"zhangsan";
NSString *key2 = @"password";
NSString *msg2 = @"123456789";
NSDictionary *paramater = @{key1:msg1,key2:msg2};
/* @param urlString 接口
* @param fileDict 文件字典
* @param fileKey 服务器接受文件的key值
* @param paramaters 普通文本信息字典
* @param success 成功之后的回调
* @param fail 失败之后的回调 */
[[CZNetworkTool sharedNetworkTool] POSTFileAndMsgWithUrlString:@"http://127.0.0.1/httpLogin/post/upload-m.php" FileDict:fileDict fileKey:@"userfile[]" paramater:paramater Success:^(id responseObject, NSURLResponse *response) {
// responseObject 解析完毕之后的数据: OC 数据
NSLog(@"请求成功: responseObject %@",responseObject);
} fail:^(NSError *error) {
//
NSLog(@"网络错误!");
}];
}
//单文件POST上传
-(void)demo1{
// 文件上传: 1.实例化工具类 2.直接上传
CZNetworkTool *tool = [CZNetworkTool sharedNetworkTool];
/* 参数1:urlString 上传文件需要的接口
* 参数2:filePath 需要上传的文件路径
* 参数3:fileKey 服务器接受文件的key值
* 参数4:fileName 文件在服务器保存的名称
* Block 类型的参数比较特殊: 可以直接执行这个 Block
* SuccessBlock:(SuccessBlock(参数类型))Success 形参
* FailBlock:(failBlock(参数类型))fail 形参 */
[tool POSTFileWithUrlString:@"http://127.0.0.1/httpLogin/post/upload.php" FilePath:@"/Users/liaojianguo/Desktop/Contents.json" FileKey:@"userfile" FileName:nil SuccessBlock:^(NSData *data, NSURLResponse *response) {
//
NSLog(@"请求成功");
} FailBlock:^(NSError *error) {
//
NSLog(@"网络链接错误");
}];
}