converting image to base64 and uploading in JSON format to server
Asked Answered
J

1

1

I have a problem. I need to convert base64 string to JSON string and pass it to server. for example I have a base64 string /9j/4AAQSkZJRgABAQAAAQABAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAACqADAAQAAAABAAAACgAAAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/Z

i need to convert it to JSON format. I do the following:

+(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;
};

here how I'm making 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];
    //....
}

But I get "Invalid length for a Base-64 char array" from server. What's wrong? If I paste my token and JSON to http://hurl.it/ and make request using it - everything goes normally.

I think the problem is / symbols in base64 string and as a result / symbols in JSON. Maybe it is something with [postData length]: if I erase \/ characters from JSON string: 9j4AAQSkZJRgABAQAAAQABAAD4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAACqADAAQAAAABAAAACgAAAAD2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQHZ request will perform normally but this base64 encoded string is not the same.

Please, help me to solve this problem

Japhetic answered 9/5, 2012 at 21:26 Comment(1)
Log post and/or request. Heck, log anything you suspect might not be right.Omen
E
5

jString is your base64 string, first use following line

[self encodeString:jString];

and then call use following.

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;
}

Hope it will work.

Embalm answered 30/1, 2013 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.