MacBoy
MacBoy

Reputation: 11

Get Corrupt data from server while upload image

I get success in converting image to base64 string but when i serve to server, server get corrupt data I am using http://codebeautify.org/base64-to-image-converter# for get proper output of image

 NSString *base64String=[UIImagePNGRepresentation(ProfileImageView.image)
    base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];
 [base64String stringByReplacingOccurrencesOfString:@"\n\r" withString:@""];
 [base64String stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];  


   NSString *post =[[NSString alloc] initWithFormat:@"UserId=%@&UserName=%@&Password=%@&Email=%@&MobileNo=%@&Profiledata=%@",[_DicData valueForKey:@"userId"],[_DicData valueForKey:@"userName"],[_DicData valueForKey:@"password"] ,[_DicData valueForKey:@"email"],MobileNumberLable.text,base64String];

    NSString *strurl2=@"api/UpdateUserProfledata/";
    NSString *strurl=[NSString stringWithFormat:@"%@%@",_Url1,strurl2];
    NSURL *url=[NSURL URLWithString:strurl];
    NSData *postData= [post dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *req=[[NSMutableURLRequest alloc]initWithURL:url];

    NSString *authtoken=[NSString stringWithFormat:@"ios %@",[_DicData valueForKey:@"authToken"]];
    [req setValue:authtoken forHTTPHeaderField:@"Authorization"];

    [req setHTTPMethod:@"POST"];
    [req setHTTPBody:postData];
    NSHTTPURLResponse *urlResponse = nil;
    NSError *err = [[NSError alloc] init];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:&urlResponse error:&err];
    NSLog(@"%@",[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding]);

When i convert image from my side i get Total Character =133624 And Total Word=0 But when server convert it get Total Character =133624 And Total Word=2891 Help Me!

Backend code for image converter in .net

public UserViewModel UpdateUserProfiledata(UserViewModel userupdate) { UserViewModel model = new UserViewModel(); if (userupdate != null && userupdate.UserId > 0) { var getuser = _unitOfWork.UserRepository.GetMany(x => x.UserId == userupdate.UserId).ToList(); if (getuser.Count() > 0) { foreach (var item in getuser) { item.UserName = userupdate.UserName; item.Email = userupdate.Email; item.MobileNo = userupdate.MobileNo; if (!string.IsNullOrEmpty(userupdate.Profiledata)) { var img = GetBytes(userupdate.Profiledata); item.ProfilePhoto = img; } model.Profiledata = userupdate.Profiledata.Trim(); _unitOfWork.UserRepository.Update(item); _unitOfWork.Save(); return model; } } } return model; } static byte[] GetBytes(string str) { byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); return bytes; }

Upvotes: 0

Views: 93

Answers (1)

Bhadresh Mulsaniya
Bhadresh Mulsaniya

Reputation: 2640

NSData *dataImage=UIImageJPEGRepresentation(yourImage, 1);
NSString *base64String = [self base64forData:dataImage];

Pass this base64String to server in your request.

Try this for base64 encoding:

- (NSString*)base64forData:(NSData*)theData
{
    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];

    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;

    NSInteger i;
    for (i=0; i < length; i += 3) {
        NSInteger value = 0;
        NSInteger j;
        for (j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }

        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] =                    table[(value >> 18) & 0x3F];
        output[theIndex + 1] =                    table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }

    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] ;
}

Upvotes: 0

Related Questions