使用二进制图像数据更新CakePHP Web服务

时间:2012-01-09 22:25:19

标签: objective-c web-services cocoa cakephp base64

我有一个Web服务(使用CakePHP),我正在使用Mac OS X应用程序成功发布字符串数据。

最初,我得到了一些建议,我需要创建一个Base 64编码的字符串,以便将PNG图像传递给CakePHP。所以我的计划是简单地生成一个URL编码的字符串(参见下面的代码示例)。为此,我已经合并了一个处理base64编码和解码的第三方类。

然而,似乎CakePHP update()函数正在从该基本64位编码的字符串中删除一些字符:

Original string may contain something like:
5nd/9ne/+zuPelWPFYtGrRpde2tlK3eXwv/

But the one that is actually stored and eventually retrieved may be:
5nd/9ne/ zuPelWPFYtGrRpde2tlK3eXwv/

很奇怪,对吗?我对CakePHP的工作原理知之甚少,所以我希望这可能是一个简单的答案。似乎我应该能够将基本64位编码的字符串发布到CakePHP - 并且它应该在不解释/删除代码中的任何字符的情况下进行更新。有什么建议吗?

以下是我将我的值发布到CakePHP网络服务的代码片段:

- (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:@"&data[company]=%@", [company_field stringValue]];
[post_values appendFormat:@"&data[phone]=%@", [phone_field stringValue]];
[post_values appendFormat:@"&data[address1]=%@", [address1_field stringValue]];
[post_values appendFormat:@"&data[address2]=%@", [address2_field stringValue]];
[post_values appendFormat:@"&data[city]=%@", [city_field stringValue]];
[post_values appendFormat:@"&data[state]=%@", [state_field stringValue]];
[post_values appendFormat:@"&data[zip]=%@", [zip_field stringValue]];
[post_values appendFormat:@"&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:@"&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);

}

1 个答案:

答案 0 :(得分:0)

这绝对是一个空间的Objective-C转换。它的cakephp方面不会触及base64编码上的任何内容。