iOS开发之多文件上传
//
// ViewController.m
// B03-多文件上传
//
// Created by 0426iOS on 15/7/1.
// Copyright (c) 2015年 0426iOS. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSString *strUrl = @"http://127.0.0.1/upload/upload-m.php";
//加载两个文件
NSString *fileName1 = @"oo.png";
NSString *path1 = [[NSBundle mainBundle] pathForResource:fileName1 ofType:nil];
NSData *data1 = [NSData dataWithContentsOfFile:path1];
NSString *fileName2 = @"xx.png";
NSString *path2 = [[NSBundle mainBundle] pathForResource:fileName2 ofType:nil];
NSData *data2 = [NSData dataWithContentsOfFile:path2];
NSDictionary *files = @{fileName1:data1,fileName2:data2};
NSDictionary *params = @{@"username":@"hhhhhhhhhhh"};
//上传多个文件
[self uploadFiles:strUrl files:files name:@"userfile[]" params:params];
}
#define kBOUNDARY @"aabbcc"
//上传多个文件
- (void)uploadFiles:(NSString *)strUrl files:(NSDictionary *)files name:(NSString *)name params:(NSDictionary *)param{
NSURL *url = [NSURL URLWithString:strUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"post";
//Content-Type:multipart/form-data; boundary=----WebKitFormBoundarycwuJbGNWchuHGPRT
NSString *ct = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",kBOUNDARY];
[request setValue:ct forHTTPHeaderField:@"Content-Type"];
//
request.HTTPBody = [self makeBody:files name:name params:param];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//
if (!connectionError) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200) {
//
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
NSLog(@"%@",json);
}else{
NSLog(@"服务器内部错误");
}
}else{
NSLog(@"请求错误%@",connectionError);
}
}];
}
//
- (NSData *)makeBody:(NSDictionary *)files name:(NSString *)name params:(NSDictionary *)param{
NSMutableData *mData = [NSMutableData data];
//准备文件
[files enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSData *data, BOOL *stop) {
// ------WebKitFormBoundarycwuJbGNWchuHGPRT
// Content-Disposition: form-data; name="userfile[]"; filename="Snip20150701_10.png"
// Content-Type: image/png
//1
NSMutableString *mStr = [NSMutableString string];
[mStr appendFormat:@"\r\n--%@\r\n",kBOUNDARY];
[mStr appendFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",name,key];
[mStr appendString:@"application/octet-stream\r\n"];
[mStr appendString:@"\r\n"];
[mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];
//2
[mData appendData:data];
}];
//准备表单中的数据
[param enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
// ------WebKitFormBoundarycwuJbGNWchuHGPRT
// Content-Disposition: form-data; name="username"
//
// 345345
NSMutableString *mStr = [NSMutableString string];
[mStr appendFormat:@"\r\n--%@\r\n",kBOUNDARY];
[mStr appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n",key];
[mStr appendString:@"\r\n"];
[mStr appendFormat:@"%@\r\n",value];
[mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];
}];
NSString *end = [NSString stringWithFormat:@"--%@--\r\n",kBOUNDARY];
[mData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
return mData.copy;
}
@end
最新文章
- 在 windows 环境下安装 redislive
- SAP常用命令及BASIS操作
- linux cp命令参数及用法详解
- Java面向对象的继承
- .NET: C#: Attribute
- Listview的闪烁问题
- (转)如何构建高性能,稳定SOA应用之-负载均衡-Decoupled Invocation(一)
- php实现MVC
- .Net Core迁移到MSBuild的多平台编译问题
- MySQL调优三步曲(慢查询、explain profile)
- ETL作业调度工具TASKCTL的两个重大突破
- 静态频繁子图挖掘算法用于动态网络——gSpan算法研究
- python中的None
- jQuery第七章插件的编写和使用
- 前端了解即可:OSS客户端如何使用,以实现资源分离
- libunistring-0.9.9
- 【转】嵌入式系统 Boot Loader 技术内幕,带你完全了解Boot Loader
- 将项目托管到GitHub实现步骤
- 在OpenShift上托管web.py应用
- android 中的 window,view,activity具体关系
热门文章
- 【树状数组】Codeforces Round #755 D. PolandBall and Polygon
- Java小问题的解决方法系列
- KEIL3中出现的字符不对齐的情况解决办法
- 【jQuery】:not选择器的说明和:checked选择器的使用
- 自定义WebViewPage,实现Url.Action生成绝对地址
- websocket 和 socket.io 之间的区别
- Redis使用记录
- linux下javadoc生成文件出现中文乱码
- 传统项目目录结构下maven+junit+junitReport
- Node.js 使用爬虫批量下载网络图片到本地