IOS上传图片/文件的通用方法(含时间戳验证和Sign签名)

分类:手机开发| 发布:llmaomi| 查看: | 发表时间:2015/5/8
 (NSString*) memberUploadavatar:(UIImage *)image {
    NSString *action = @"action=uploadavatar";
    NSString *mainUrl = [self getMainUrl];
    NSString *unixTime = [NSString stringWithFormat:@"timestamp=%ld", (time_t) [[NSDate date] timeIntervalSince1970]];
//    NSString *unixTime = @"timestamp=1429629388";
    NSMutableArray *nameList = [[NSMutableArray alloc] init];
    [nameList addObject:unixTime];
    [nameList addObject:action];
    NSString *sign = [self generateSign:mainUrl nameList:nameList];
    NSString *url = [NSString stringWithFormat:@"%@?%@&%@&sign=%@", mainUrl, action, unixTime, sign];
    
    //要上传的图片
//    UIImage *image=[params objectForKey:@"pic"];
    //分界线的标识符
    NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
    //根据url初始化request
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                       timeoutInterval:10];
    //分界线 --AaB03x
    NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
    //结束符 AaB03x--
    NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];

    //得到图片的data
    NSData* data = UIImagePNGRepresentation(image);
    //http body的字符串
    NSMutableString *body=[[NSMutableString alloc]init];
    
    ////添加分界线,换行
    [body appendFormat:@"%@\r\n",MPboundary];
    //声明pic字段,文件名为boris.png
    [body appendFormat:@"Content-Disposition: form-data; name=\"Image\"; filename=\"boris.png\"\r\n"];
    //声明上传文件的格式
    [body appendFormat:@"Content-Type: image/png\r\n\r\n"];
    
    //声明结束符:--AaB03x--
    NSString *end=[[NSString alloc]initWithFormat:@"\r\n%@",endMPboundary];
    //声明myRequestData,用来放入http body
    NSMutableData *myRequestData=[NSMutableData data];
    //将body字符串转化为UTF8格式的二进制
    [myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
    //将image的data加入
    [myRequestData appendData:data];
    //加入结束符--AaB03x--
    [myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
    
    //设置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", (unsigned long)[myRequestData length]] forHTTPHeaderField:@"Content-Length"];
    //设置http body
    [request setHTTPBody:myRequestData];
    //http method
    [request setHTTPMethod:@"POST"];
    
    NSError *error;
    NSData *resdata = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    NSString *string = [[NSString alloc] initWithData:resdata encoding:NSUTF8StringEncoding];
    DLog(@"%@", string);
    
    _errMsg = @"";
    NSDictionary *jsonDataDict = [string JSONValue];
    NSDictionary *jsonData = [jsonDataDict objectForKey:@"status"];
    NSNumber *code = [jsonData objectForKey:@"code"];
    if ([code intValue] == -1) {
        // 上传头像失败
        _errMsg =  [jsonData objectForKey:@"msg"];
    } else {
        // 上传头像成功
        NSDictionary *jsonResult =  [jsonDataDict objectForKey:@"result"];
        NSString *avatar = [jsonResult objectForKey:@"avatar"];
        return avatar;
    }
    
    return nil;

365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/mobile-dev/1996.html