Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating CakePHP web service with binary image data
    primarykey
    data
    text
    <p>I have a web service (which uses CakePHP) that I am posting string data to successfully with a Mac OS X app I'm working on. </p> <p>Initially, I was given some advice that I would need to create a Base 64 encoded string to pass over a PNG imageto CakePHP. So my plan was to simply generate a URL encoded string (see code sample below). To do that, I've incorporated a third-party class which handles the base64 encode and decode. </p> <p>However, it seems like the CakePHP update() function is removing some characters from that base 64 encoded string:</p> <pre><code>Original string may contain something like: 5nd/9ne/+zuPelWPFYtGrRpde2tlK3eXwv/ But the one that is actually stored and eventually retrieved may be: 5nd/9ne/ zuPelWPFYtGrRpde2tlK3eXwv/ </code></pre> <p>Weird, right? I'm working on very limited knowledge of how CakePHP works, so I'm hoping this may be a simple answer. It seems like I should be able to post that base 64 encoded string to CakePHP - and that it should update without interpreting/removing any characters from the code. Any advice?</p> <p>Here is the code snippet of how I'm posting my values to the CakePHP web service:</p> <pre><code>- (void) updateApi { NSLog(@"Updating user account settings"); NSString* request_url = @"/users/update"; NSLog(@"%@", [NSString stringWithFormat:@"%@%@", @"http://somesite.com", request_url]); NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", @"http://somesite.com", request_url]]]; NSMutableString* post_values = [NSMutableString stringWithFormat:@"data[id]=%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"]]; [post_values appendFormat:@"&amp;data[company]=%@", [company_field stringValue]]; [post_values appendFormat:@"&amp;data[phone]=%@", [phone_field stringValue]]; [post_values appendFormat:@"&amp;data[address1]=%@", [address1_field stringValue]]; [post_values appendFormat:@"&amp;data[address2]=%@", [address2_field stringValue]]; [post_values appendFormat:@"&amp;data[city]=%@", [city_field stringValue]]; [post_values appendFormat:@"&amp;data[state]=%@", [state_field stringValue]]; [post_values appendFormat:@"&amp;data[zip]=%@", [zip_field stringValue]]; [post_values appendFormat:@"&amp;data[website]=%@", [website_field stringValue]]; // Iteration 09 - Store logo as PNG and prepare to upload to server NSImage *logoImage = [logo_image image]; if (logoImage) { // Image exists; we need to convert it to a BLOB for our back-end database posting NSLog(@"Image exists in logo_image"); // Grab possible representations of this image NSArray *representations = [logoImage representations]; // Create the PNG data NSData *imageData = [NSBitmapImageRep representationOfImageRepsInArray:representations usingType:NSPNGFileType properties:nil]; // Base 64 encoding should happen here NSString *base64encodedImageData = [imageData base64Encoding]; // Convert to base 64 string NSLog(@"\n\nBase 64 image data encoded as \n\n%@\n\n", base64encodedImageData); // Store data in parameter [post_values appendFormat:@"&amp;data[logo_png_data]=%@", base64encodedImageData]; } NSString *encoded_post_values = [post_values stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"\n\nEncoded post values string is \n\n%@\n\n", encoded_post_values); NSData *request_data = [NSData dataWithBytes:[encoded_post_values UTF8String] length:[encoded_post_values length]]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; [request setHTTPBody:request_data]; NSData* return_data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //NSLog(@"%@", [[NSString alloc] initWithData:return_data encoding:NSUTF8StringEncoding]); NSString* result = [[CJSONDeserializer deserializer] deserialize:return_data error:nil]; //NSLog(@"%@", result); </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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