将图像转换为base64并以JSON格式上传到服务器

时间:2012-05-09 21:26:02

标签: iphone objective-c ipad base64 nsurlrequest

我有问题。我需要将base64字符串转换为JSON字符串并将其传递给服务器。 例如,我有一个base64字符串/9j/4AAQSkZJRgABAQAAAQABAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAACqADAAQAAAABAAAACgAAAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/Z

我需要将其转换为JSON格式。我做了以下事情:

+(NSData *)prepareForUploading:(NSString *)base64Str
{
    NSDictionary *dict=[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:base64str, nil] forKeys:[NSArray arrayWithObjects:@"picture", nil]];

    NSData *preparedData=[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];    
    return preparedData;
};

这里我是如何制作NSURLRequest

-(NSString *)uploadPict:(NSString *)pict
{
    NSLog(@"Server: upload: called");
    NSData *prepPictData=[[self class] prepareForUploading:pict];
    NSString *preparedBase64StrInJSON=[[NSString alloc] initWithData:prepPictData encoding:NSUTF8StringEncoding];

    //here I'm adding access token to request
    NSString *post = [NSString stringWithFormat:@"accessToken=%@&object=%@", self.key, preparedBase64StrInJSON];
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@upload.aspx", serverAPIPath]]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"postLength" forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];    

    NSURLResponse *response;
    NSError *error;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    //....
}

但是我从服务器得到“Base-64 char数组的长度无效”。怎么了? 如果我将我的令牌和JSON粘贴到http://hurl.it/并使用它进行请求 - 一切正常。

我认为问题是base64字符串中的/符号以及JSON中的结果/符号。 也许它与[postData length]有关:如果我从JSON字符串中删除\/个字符: 9j4AAQSkZJRgABAQAAAQABAAD4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAACqADAAQAAAABAAAACgAAAAD2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQHZ请求将正常执行,但此base64编码的字符串不相同。

请帮我解决这个问题

1 个答案:

答案 0 :(得分:5)

jString是你的base64字符串,首先使用以下行

[self encodeString:jString];

然后调用以下。

NSString *URL = [NSString stringWithFormat:@"forms.asmx/CreateUpdate?"];

URL=[NSString stringWithFormat:@"%@%@", USERS_API_ROOT_URL, URL];
NSString *post = [NSString stringWithFormat:@"apiKey=A0B1I2L3A4L5-A1D3-4F30-5AB2-C8DEE266&strPost=%@",jString];

unsigned long long postLength = [post length];
NSString *contentLength = [NSString stringWithFormat:@"%llu",postLength];

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:URL]];
[request setHTTPMethod:@"POST"];
[request setValue:contentLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData ];

(void)[[NSURLConnection alloc] initWithRequest:request  delegate:self];


-(NSString *)encodeString:(NSString *)string
{
    NSString *newString = (__bridge_transfer NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL,CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    return newString;
}

希望它能奏效。