×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Objective-C
Posted by: Black Light
Added: Mar 3, 2013 12:18 PM
Modified: Mar 15, 2013 12:04 PM
Views: 44
Tags: no tags
  1. NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@webservice.php", kServerName]];
  2.            
  3.             NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
  4.             [urlRequest setHTTPMethod:@"POST"];
  5.  
  6.     NSString *boundary=@"---------------------------14737809831466499882746641449";
  7.  
  8.  
  9. // file
  10. [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  11. [body appendData:[[NSString stringWithString:@"Content-Disposition: attachment; name=\"userfile\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  12. [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  13. [body appendData:[NSData dataWithData:imageData]];
  14. [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  15.  
  16. // text parameter
  17. [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  18. [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter1\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  19. [body appendData:[parameterValue1 dataUsingEncoding:NSUTF8StringEncoding]];
  20. [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  21.  
  22. // another text parameter
  23. [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  24. [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter2\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  25. [body appendData:[parameterValue2 dataUsingEncoding:NSUTF8StringEncoding]];
  26. [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  27.  
  28. // close form
  29. [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  30.  
  31. // set request body
  32. [request setHTTPBody:body];
  33.  
  34.  NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  35.            
  36.             [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  37.                 if ([data length] >0 && error == nil)
  38.                 {
  39.                     // if response is a string
  40.                     NSString *responseString=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  41.  
  42. // if response is JSON Array
  43.  NSMutableArray *array=[NSMutableArray arrayWithArray:[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]];
  44.                    
  45. // if response is JSON Object
  46.  NSMutableDictionary *dictionary=[NSMutableDictionary dictionaryWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]];
  47.                 }
  48.                 else if ([data length] == 0 && error == nil)
  49.                 {
  50.                     NSLog(@"Nothing was downloaded.");
  51.                 }
  52.                 else if (error != nil)
  53.                 {
  54.                     NSLog(@"Error happened = %@", error);
  55.                 }
  56.             }];