Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading image to server with base64 haxcode with userid parameter in iPhone

Tags:

ios

iphone

I'm using this code but problem is that it encodes the Haxcode in nsdata conversion block I want to send same has code which I'm getting with userid which is fixed integer. Please help.

 NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image1, 0)];
    //image.image=image1;

    [Base64 initialize];
    NSString *b64EncStr = [Base64 encode:imageData];

    NSLog(@"encoded.%@",b64EncStr);




     NSURL *url = [[NSURL alloc] initWithString:updateimageURL];
    NSMutableURLRequest *req = [[NSMutableURLRequest alloc]initWithURL:url];
    [req setHTTPMethod:@"POST"];

    NSString *trimmed = [b64EncStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];



    paramDataString = [NSString stringWithFormat:@"Id=%d&FromString=%@",100,trimmed];

    NSLog(@"%@",paramDataString);


    NSData* aData = [paramDataString dataUsingEncoding:NSUTF8StringEncoding];


      [req setHTTPBody: aData];

    NSURLConnection *theConnection=[[NSURLConnection alloc]initWithRequest:req delegate:self];
    if (theConnection)
    {
        NSMutableData *data = [[NSMutableData alloc] init];
        self.receivedData=data;
        [data release];

    }
    else {

        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:@"Check your networking configuration." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }

    [url release];
    [req release];

It is making the URL connection in right way but response is server error.

like image 296
Charlie Avatar asked Dec 04 '22 12:12

Charlie


1 Answers

First Convert UImage to NSData and from NSData to base64string and then pass that to your Webservice

finalImagePath = [imageData base64EncodedString];

NSString *strImageData = [finalImagePath stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];

and then send strImageData to your webserivce.

While retriving from server use

NSString *strImageData = [finalImagePath stringByReplacingOccurrencesOfString:@"%2B" withString:@"+"];
like image 181
Vivek Sehrawat Avatar answered Jan 26 '23 01:01

Vivek Sehrawat