ios 七牛form表单提交图片
ios 七牛form表单提交图片
+ (NSMutableURLRequest *)postRequestWithParems:(NSDictionary *)postParems images:(UIImage*)image
{
NSString *key = SafeString([postParems objectForKey:@”key”]);
if (!key.length)
{
key = @”file”;
}
NSURL *URL = [NSURL URLWithString:@”http://up.qiniu.com”];
//根据url初始化request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URLcachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30];
//分割符
// NSString *TWITTERFON_FORM_BOUNDARY = @”0xKhTmLbOuNdArY”;
NSString *TWITTERFON_FORM_BOUNDARY = @”WebKitFormBoundaryssvXpH9pBFbBtP4B”;
//分界线 –AaB03x
NSString *MPboundary=[[NSString alloc]initWithFormat:@”–%@”,TWITTERFON_FORM_BOUNDARY];
//结束符 AaB03x–
NSString *endMPboundary=[[NSString alloc]initWithFormat:@”%@–“,MPboundary];
//http 参数body的字符串
NSMutableString *paraBody=[[NSMutableString alloc]init];
//参数的集合的所有key的集合
NSArray *keys= [postParems allKeys];
//遍历keys
for(int i = 0; i < [keys count] ; i++)
{
//得到当前key
NSString *key=[keys objectAtIndex:i];
//添加分界线,换行
[paraBody appendFormat:@”%@\r\n”,MPboundary];
//添加字段名称,换2行
[paraBody appendFormat:@”Content-Disposition: form-data; name=”%@”\r\n\r\n”,key];
//添加字段的值
[paraBody appendFormat:@”%@\r\n”,[postParems objectForKey:key]];
// DLog(@”参数%@ == %@”,key,[postParems objectForKey:key]);
}
//声明myRequestData,用来放入http body
NSMutableData *myRequestData = [[NSMutableData alloc] init];
//将body字符串转化为UTF8格式的二进制
[myRequestData appendData:[paraBody dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableString *imageBody = [[NSMutableString alloc] init];
NSData *imageData = nil;
NSString *fileNmae = nil;
NSString *name = @”file”;
NSString *type = nil;
//判断图片是不是png格式的文件
if (UIImagePNGRepresentation(image))
{
//返回为png图像。
imageData = UIImagePNGRepresentation(image);
fileNmae = [NSString stringWithFormat:@”%@.png”,key];
type = @”image/png”;
}
else
{
//返回为JPEG图像。
imageData = UIImageJPEGRepresentation(image, 1.0);
fileNmae = [NSString stringWithFormat:@”%@.jpg”,key];
type = @”image/jpeg”;
}
//添加分界线,换行
[imageBody appendFormat:@”%@\r\n”,MPboundary];
//声明pic字段,文件名为boris.png
[imageBody appendFormat:@”Content-Disposition: form-data; name=”%@”; filename=”%@”\r\n”,name,fileNmae];//\r\n
//声明上传文件的格式
[imageBody appendFormat:@”Content-Type: %@\r\n\r\n”,type];
//Content-Type: image/jpge,image/gif, image/jpeg, image/pjpeg, image/png, image/pjpeg\r\n\r\n
//Content-Type: application/octet-stream\r\n\r\n
//将image的data加入
[myRequestData appendData:[imageBody dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[[NSData alloc] initWithData:imageData]];
[myRequestData appendData:[@”\r\n” dataUsingEncoding:NSUTF8StringEncoding]];
//声明结束符–AaB03x–
NSString *end=[[NSString alloc]initWithFormat:@”\r\n%@”,endMPboundary];
//加入结束符–AaB03x–
[myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
NSString *datas = nil;
datas = [NSString stringWithFormat:@”%@%@图片图片\r\n%@”,paraBody,imageBody,end]
DLog(@”datas = \n %@”,datas);
//设置HTTPHeader中Content-Type的值
NSString *content=[[NSString alloc]initWithFormat:@”multipart/form-data; boundary=%@”,TWITTERFON_FORM_BOUNDARY];
//设置HTTPHeader
[request setValue:content forHTTPHeaderField:@”Content-Type”];
//设置Content-Length
[request setValue:[NSString stringWithFormat:@”%lu”, [myRequestData length]]forHTTPHeaderField:@”Content-Length”];
//设置http body
[request setHTTPBody:myRequestData];
//http method
[request setHTTPMethod:@”POST”];
return request;
}
提交的必要参数token再就是图片了。相关文档去官网上找吧。
当我整出来后又出现一个问题,上传图片是成功了,但图片的外连接无法查看图片,找各种原因啊,改着测试啊,后来发现拼form结构的时候图片的多个信息时多了空行,只要换行就行了不能空出来多余的,来张图片解释一下。(之前多了\r\n,上面的代码是修改之后的)
描述和类型等多个信息不能有空行,当最后一个信息和真正的图片data是要有空行的,这样就解决了上传图片七牛服务器成功后,图片无法显示的问题。
生成token的ios代码如下,
+ (NSString *)qiniutoken:(NSString *)key
{
NSDate *delay = [NSDate dateWithTimeInterval:60*60 sinceDate:[NSDate date]];
NSString *delayString = [NSString stringWithFormat:@”%ld”,(unsigned long)[delaytimeIntervalSince1970]];
NSString *ak = @”eW0C4LihuAMgPaIqY49OiP34JSUokhhtfyiMoHigQ_qLf”;
NSString *sk = @”y4R3ply_Nk4EkOkXlyokhYJjc684Np06UplKO-IPK201cFS”;
NSString *bn = @”adong-test-pics”;
NSString *scope = key.length ? [NSString stringWithFormat:@”%@:%@”,bn,key] : bn;
NSString *putPolicyStr = [NSString stringWithFormat:@”{“scope”:”%@”,”deadline”:%@}”,scope,delayString];
NSString *encoded = [putPolicyStr base64EncodedString];
NSString *encode_signed = [Common hmacsha1:encoded key:sk];
NSString *token = [NSString stringWithFormat:@”%@:%@:%@”,ak,encode_signed,encoded];
DLog(@”\n putPolicyStr = %@ \n encoded = %@ \n encode_signed = %@ \n token = %@”,putPolicyStr,encoded,encode_signed,token);
return token;
}
ios hmacsha1这个算法就贴代码了网上有很多,太长了还要涉及多个base64文件。ak、sk、bn(空间名)的值已经被改动了,不能直接使用,自己去申请使用,是免费的。
key是非必要参数,有的话上传成功后这个公开七牛云服务器地址再加上key就可以访问了,官网都有介绍。