converting image to base64 and uploading in JSON format to server

女生的网名这么多〃 提交于 2019-11-29 16:48:06
aBilal17

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!