多图表单上传
来源:互联网 发布:电路板设计软件下载 编辑:程序博客网 时间:2024/06/02 04:15
@implementation PicUpload
static NSString * const FORM_FLE_INPUT = @"file1";
+ (NSString *)postRequestWithURL: (NSString *)url
postParems: (NSMutableDictionary *)postParems
picFilePath: (NSMutableArray *)picFilePath
picFileName: (NSMutableArray *)picFileName
{
NSString *hyphens = @"--";
NSString *boundary = @"*****";
NSString *end = @"\r\n";
NSMutableData *myRequestData1=[NSMutableData data];
//遍历数组,添加多张图片
for (int i = 0; i < picFilePath.count; i ++) {
NSData* data;
UIImage *image=[UIImage imageWithContentsOfFile:[picFilePath objectAtIndex:i]];
//判断图片是不是png格式的文件
if (UIImagePNGRepresentation(image)) {
//返回为png图像。
data = UIImagePNGRepresentation(image);
}else {
//返回为JPEG图像。
data = UIImageJPEGRepresentation(image, 1.0);
}
//所有字段的拼接都不能缺少,要保证格式正确
[myRequestData1 appendData:[hyphens dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData1 appendData:[boundary dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData1 appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableString *fileTitle=[[NSMutableString alloc]init];
//要上传的文件名和key,服务器端用file接收
[fileTitle appendFormat:@"Content-Disposition:form-data;name=\"%@\";filename=\"%@\"",[NSString stringWithFormat:@"file%d",i+1],[NSString stringWithFormat:@"image%d.png",i+1]];
[fileTitle appendString:end];
[fileTitle appendString:[NSString stringWithFormat:@"Content-Type:application/octet-stream%@",end]];
[fileTitle appendString:end];
[myRequestData1 appendData:[fileTitle dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData1 appendData:data];
[myRequestData1 appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
}
[myRequestData1 appendData:[hyphens dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData1 appendData:[boundary dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData1 appendData:[hyphens dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData1 appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
//参数的集合的所有key的集合
NSArray *keys= [postParems allKeys];
//添加其他参数
for(int i=0;i<[keys count];i++)
{
NSMutableString *body=[[NSMutableString alloc]init];
[body appendString:hyphens];
[body appendString:boundary];
[body appendString:end];
//得到当前key
NSString *key=[keys objectAtIndex:i];
//添加字段名称
[body appendFormat:@"Content-Disposition: form-data; name=\"%@\"",key];
[body appendString:end];
[body appendString:end];
//添加字段的值
[body appendFormat:@"%@",[postParems objectForKey:key]];
[body appendString:end];
[myRequestData1 appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"添加字段的值==%@",[postParems objectForKey:key]);
}
//根据url初始化request
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:20];
//设置HTTPHeader中Content-Type的值
NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",boundary];
//设置HTTPHeader
[request setValue:content forHTTPHeaderField:@"Content-Type"];
//设置Content-Length
[request setValue:[NSString stringWithFormat:@"%d", [myRequestData1 length]] forHTTPHeaderField:@"Content-Length"];
//设置http body
[request setHTTPBody:myRequestData1];
//http method
[request setHTTPMethod:@"POST"];
NSHTTPURLResponse *urlResponese = nil;
NSError *error = [[NSError alloc]init];
NSData* resultData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponese error:&error];
NSString* result= [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
if([urlResponese statusCode] >=200&&[urlResponese statusCode]<300){
NSLog(@"返回结果=====%@",result);
SBJsonParser *parser = [[SBJsonParser alloc ] init];
NSDictionary *jsonobj = [parser objectWithString:result];
if (jsonobj == nil || (id)jsonobj == [NSNull null] || [[jsonobj objectForKey:@"flag"] intValue] == 0)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"提交失败." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
});
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"提交成功." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[Singleton sharedSingleton].shopId = [[jsonobj objectForKey:@"shopId"]stringValue];
[alert show];
});
}
return result;
}
else if (error) {
NSLog(@"%@",error);
[[NSNotificationCenter defaultCenter]postNotificationName:@"dissmissSVP" object:nil];
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"提交失败." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
return nil;
}
else
return nil;
}
@end
- 多图表单上传
- 单/多文件上传
- 单多文件上传
- 单文件上传、多文件上传
- 单文件上传和多文件上传
- php文件单上传和多上传
- OkhttpUtils单、多文件上传
- echarts学习笔记(2) ---- 设置单页面多张图表
- (转载)struts2利用自带的上传拦截器 进行多图表和文本上传
- 文件上传 单文件 多文件 皆可
- struts2单文件与多文件上传
- 【Struts2】单文件、多文件上传
- AFNETWORKING上传单图片/多图片
- Struts1单文件、多文件上传
- Struts2单文件、多文件上传
- PHP单文件/多文件上传
- struts2单文件,多文件上传
- [struts2]单文件与多文件上传
- 单链表实现管理系统
- 小型web服务器thttpd的学习总结(上)&小型web服务器thttpd的学习总结(下)
- ITIL介绍
- 初识Node.js<一>DNS查询的例子
- H3C 交换机端口汇聚
- 多图表单上传
- java学习笔记1--开发环境平台总结
- Installtion ID : UUID 统计安装
- Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法
- 序列化和反序列化系列博客(一)—— What and Why?
- 结构【重学C之六】
- ref和out的使用与区别
- (图文)在MyEclipse(2015)中上传项目到github的步骤(很详细)
- 发现群组(二)分级聚类