1,采用苹果原生NSMutableURLRequest请求
//1. NSURL
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/post/upload-m.php"];
//2.request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//设置请求方法
request.HTTPMethod = @"POST";
//设置请求头
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",kboundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
//设置请求体
request.HTTPBody = [self bodyData];
//3.建立连接发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//4. 解析数据
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",result);
}];
- (NSData *)bodyData{
//创建可变数组用来添加数据
NSMutableData *dataM = [NSMutableData data];
//添加文件的头部的文本信息
//添加第一个文件的信息
//1. 添加第一行的文本信息
NSString *fString = [NSString stringWithFormat:@"--%@\r\n",kboundary];
[dataM appendData:[fString dataUsingEncoding:NSUTF8StringEncoding]];
//2. 添加第二行的文本信息
NSString *sString = @"Content-Disposition: form-data; name=\"userfile[]\"; filename=\"2.png\"\r\n";
[dataM appendData:[sString dataUsingEncoding:NSUTF8StringEncoding]];
//3.添加第三行和四行信息
[dataM appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//4. 添加头像真正的二进制数据
NSString *path = [[NSBundle mainBundle] pathForResource:@"2.png" ofType:nil];
NSData *fileData = [NSData dataWithContentsOfFile:path];
[dataM appendData:fileData];
[dataM appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//添加第二个文件的信息
//1. 添加第一行的文本信息
NSString *fString2 = [NSString stringWithFormat:@"--%@\r\n",kboundary];
[dataM appendData:[fString2 dataUsingEncoding:NSUTF8StringEncoding]];
//2. 添加第二行的文本信息
NSString *sString2 = @"Content-Disposition: form-data; name=\"userfile[]\"; filename=\"3.jpg\"\r\n";
[dataM appendData:[sString2 dataUsingEncoding:NSUTF8StringEncoding]];
//3.添加第三行和四行信息
[dataM appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//4. 添加头像真正的二进制数据
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"3.jpg" ofType:nil];
NSData *fileData2 = [NSData dataWithContentsOfFile:path2];
[dataM appendData:fileData2];
[dataM appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//添加文本的信息
//1. 添加第一行的文本信息
NSString *contentString = [NSString stringWithFormat:@"--%@\r\n",kboundary];
[dataM appendData:[contentString dataUsingEncoding:NSUTF8StringEncoding]];
//2. 添加第二行的文本信息
[dataM appendData:[@"Content-Disposition: form-data; name=\"status\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//4. 添加文本的二进制数据
[dataM appendData:[@"你好\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//5.添加结尾
[dataM appendData:[[NSString stringWithFormat:@"--%@--",kboundary] dataUsingEncoding:NSUTF8StringEncoding]];
return dataM.copy;
}
2,采用AFN封装处理
//用文件的二进制进行文件上传
- (void)uploadFiles2{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; //发给服务器的文本信息 NSDictionary *dict = @{ @"status":@"csss" }; [manager POST:@"http://127.0.0.1/post/upload-m.php" parameters:dict constructingBodyWithBlock:^(id_Nonnull formData) {
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"2.png" ofType:nil];
NSData *data1 = [NSData dataWithContentsOfFile:path1];
//第一个参数: 文件的二进制数据
//第二个参数: 服务器接收文件的参数名
//第三个参数: 文件的名称
//第四个参数: 文件的类型
[formData appendPartWithFileData:data1 name:@"userfile[]" fileName:@"45.jpg" mimeType:@"image/png"];
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"3.jpg" ofType:nil];
NSData *data2 = [NSData dataWithContentsOfFile:path2];
//第一个参数: 文件的二进制数据
//第二个参数: 服务器接收文件的参数名
//第三个参数: 文件的名称
//第四个参数: 文件的类型
[formData appendPartWithFileData:data2 name:@"userfile[]" fileName:@"67.jpg" mimeType:@"image/png"];
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
//用文件的URL进行文件上传- (void)uploadFiles1{ AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; //发给服务器的文本信息 NSDictionary *dict = @{ @"status":@"asdfasdflasfjd;as" }; //constructingBodyWithBlock , [manager POST:@"http://127.0.0.1/post/upload-m.php" parameters:dict constructingBodyWithBlock:^(id_Nonnull formData) {
//返回工程图片的URL
NSURL *url1 = [[NSBundle mainBundle] URLForResource:@"2.png" withExtension:nil];
NSURL *url2 = [[NSBundle mainBundle] URLForResource:@"3.jpg" withExtension:nil];
//添加第一个文件到请求体重, url就是你要上传的文件的路径 name:服务器接收文件的参数名称
[formData appendPartWithFileURL:url1 name:@"userfile[]" error:NULL];
//添加第二个文件
[formData appendPartWithFileURL:url2 name:@"userfile[]" error:NULL];
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}