Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
- //Set Params
- [request setHTTPShouldHandleCookies:NO];
- [request setTimeoutInterval:60];
- [request setHTTPMethod:@"POST"];
- //Create boundary, it can be anything
- NSString *boundary = @"------fafafafafafafafafafaVohpleBoundary4QuqLuM1cE5lMwCy";
- // 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];
- //Populate a dictionary with all the regular values you would like to send.
- NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
- [parameters setValue:[NSString stringWithFormat:@"hello.png"] forKey:@"/ProfImage"];
- // add params (all params are strings)
- for (NSString *param in parameters) {
- [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", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
- }
- NSString *FileParamConstant = @"imageParamName";
- NSData *imageData = UIImageJPEGRepresentation(imgProfile.image, 1.0);
- //Assuming data is not nil we add this to the multipart form
- if (imageData)
- {
- [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[@"Content-Type:image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:imageData];
- [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
- }
- //Close off the request with the boundary
- [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
- // setting the body of the post to the request
- [request setHTTPBody:body];
- // set URL
- [request setURL:[NSURL URLWithString:@"http://72.9.100.234/pixitchphotosvc/Service1.svc"]];
- [NSURLConnection sendAsynchronousRequest:request
- queue:[NSOperationQueue mainQueue]
- completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
- NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
- NSLog(@"%d",[httpResponse statusCode]);
- if ([httpResponse statusCode] == 200) {
- NSLog(@"success");
- }
- }];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement