2012-01-09 77 views
3

我有一个web服务(使用CakePHP),我发布了字符串数据,以便正在使用我正在处理的Mac OS X应用程序。使用二进制映像数据更新CakePHP web服务

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

然而,它看起来像CakePHP的更新()函数是去除从基部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如何工作的知识,所以我希望这可能是一个简单的答案。看起来我应该能够将该base 64编码的字符串发布到CakePHP - 并且它应该更新而不解释/删除代码中的任何字符。有什么建议?

这里是如何我张贴我的价值观,以CakePHP的Web服务的代码片段:

- (void) updateApi { 

NSLog(@"Updating user account settings"); 
NSString* request_url = @"https://stackoverflow.com/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

这里发生的是+转换为空格 请参阅http://stackoverflow.com/questions/5422028/objective-c-how-to-upload-image-and-text-using-http-post – 2012-01-23 18:11:35

回答

0

这肯定是一个空间的Objective-C的转换。它的cakephp方面不会碰到base64编码的任何东西。