Note that there are some explanatory texts on larger screens.

plurals
  1. POImage post to Server with data json not working ios
    text
    copied!<p>I have tried to post image with data using the following code.Data is getting upload but image is not uploading.</p> <pre><code>-(void)imageUpload { name=@"Har9233"; userId=@"2969"; cityId=@"1"; mobile=@"9888329329"; mobileVerify=@"no "; gender=@"1"; //NSString *imageData=@"12"; NSData *imageData = UIImageJPEGRepresentation(imageView.image, 1.0); NSMutableDictionary* _params = [[NSMutableDictionary alloc] init]; [_params setObject:userId forKey:@"userId"]; [_params setObject:name forKey:@"profileDisplayName"]; [_params setObject:gender forKey:@"gender"]; [_params setObject:cityId forKey:@"cityId"]; [_params setObject:mobile forKey:@"mobile"]; [_params setObject:mobileVerify forKey:@"isMobileVerified"]; NSString *boundary = @"ghkyre–nhjfhdj-74f5f-gfg5-gggff"; NSString* FileParamConstant =@"image"; // NSURL* requestURL = [NSURL URLWithString:@"myurl"]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; [request setHTTPShouldHandleCookies:NO]; [request setTimeoutInterval:30]; [request setHTTPMethod:@"POST"]; // set Content-Type in HTTP header NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request setValue:contentType forHTTPHeaderField: @"Content-Type"]; // post body NSMutableData *body = [NSMutableData data]; // add params (all params are strings) for (NSString *param in _params) { [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]]; } // add image data if (imageData) { NSLog(@"2"); [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"harryimg1.png\"\r\n",FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imageData]]; [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; } // [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // setting the body of the post to the reqeust [request setHTTPBody:body]; // set the content-length NSString *postLength = [NSString stringWithFormat:@"%d", [body length]]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; // set URL [request setURL:requestURL]; NSURLResponse *response; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:Nil]; NSDictionary *jsonResponseData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil]; NSLog(@"DATA=%@",jsonResponseData); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload