Guest User

Ios WEb Service Communication

a guest
Jan 1st, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. I am trying to upload an image from my device to the server and each time the else condition executes... Please help me sending the right request from device to server
  2. <?php
  3.  
  4. include 'colorsofimage.php';
  5. include 'include/functions.php';
  6.  
  7. $base = $_REQUEST["image"];
  8.  
  9. if (isset($base)) {
  10.  
  11.     $image_name = "img_" . date("Y-m-d-H-m-s") . ".jpg";
  12.     $binary = base64_decode($base);
  13.     // binary, utf-8 bytes
  14.     header("Content-Type: bitmap; charset=utf-8");
  15.     $file = fopen("images/post_images/" . $image_name, "wb");
  16.     if($file){
  17.         echo 'sucess';
  18.     }else{
  19.         echo 'error';
  20.     }
  21.     die();
  22.    
  23.     fwrite($file, $binary);
  24.     fclose($file);
  25.     mysql_connect('localhost', 'root', '');
  26.     mysql_select_db('colorsearch');
  27.     mysql_query("INSERT INTO image(name)VALUE('$image_name')");
  28.     $last_id = mysql_insert_id();
  29.     $src = 'images/post_images/' . $image_name;
  30.     $functions = new ColorsConversion();
  31.     $image = new ColorsOfImage($src);
  32.     $colors = $image->getProminentColors();
  33.     $background = $image->getBackgroundColor();
  34.     foreach ($colors as $color) {
  35. //  mysql_query("INSERT INTO image_color(image_color,image_id) VALUE('$color','$last_id')");
  36.  
  37.         $rgb = $functions->hex2rgb($color);
  38.         $hsv = $functions->RGBtoHSV($rgb[0], $rgb[1], $rgb[2]);
  39. //        
  40. //        $rgb = hex2rgb($color);
  41. //        $hsv = RGBtoHSV($rgb[0], $rgb[1], $rgb[2]);
  42.         mysql_query("INSERT INTO image_color(image_color,image_background,image_color_r,image_color_g,image_color_b,image_color_h,image_color_s,image_color_v,image_id) VALUE('$color','$background','$rgb[0]','$rgb[1]','$rgb[2]','$hsv[0]','$hsv[1]','$hsv[2]','$last_id')");
  43.     }
  44.     die($image_name);
  45. } else {
  46.  
  47.     die("No POST");
  48. }
  49. ?>
  50.  
  51.  
  52. And here is my code for uploading image
  53.  
  54. - (IBAction)btnUpload:(id)sender {
  55.    
  56.     if (self.imageViewGallery.image == nil) {
  57.         UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Wait"
  58.                                                              message:@"Please Select An Image To Upload." delegate:nil
  59.                                                    cancelButtonTitle:@"OK"
  60.                                                    otherButtonTitles:nil, nil];
  61.         [ErrorAlert show];
  62.         NSLog(@"error");
  63.     }
  64.     else{
  65.     NSData *imageData = UIImageJPEGRepresentation(self.imageViewGallery.image, 90);
  66.     NSString *urlString = @"http://localhost/ColorPicker/api.upload.php";
  67.    
  68.    
  69.     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  70.     [request setURL:[NSURL URLWithString:urlString]];
  71.        
  72.     [request setHTTPMethod:@"POST"];
  73.     NSString *boundary = @"---------------------------14737809831466499882746641449";
  74.     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
  75.     [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
  76.    
  77.    
  78.     NSMutableData *body = [NSMutableData data];
  79.     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  80.     [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"iphonefile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  81.     [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  82.     [body appendData:[NSData dataWithData:imageData]];
  83.     [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  84.     [request setHTTPBody:body];
  85.    
  86.    
  87.     NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  88.     NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
  89.    
  90.     NSLog(@"returnstring is : %@",returnString);
  91.     if (returnString != nil) {
  92.         UIAlertView *uploadAlert = [[UIAlertView alloc] initWithTitle:@"Hooray!"
  93.                                                              message:@"Your Image is uploaded" delegate:nil
  94.                                                    cancelButtonTitle:@"OK"
  95.                                                    otherButtonTitles:nil, nil];
  96.         [uploadAlert show];
  97.  
  98.     }
  99.     else
  100.     {
  101.         UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Oops"
  102.                                                              message:@"Image Couldn't uploaded." delegate:nil
  103.                                                    cancelButtonTitle:@"OK"
  104.                                                    otherButtonTitles:nil, nil];
  105.         [ErrorAlert show];
  106.  
  107.    
  108.     }
  109.     }
  110.    
  111. }
Advertisement
Add Comment
Please, Sign In to add comment