Can not upload picture using Base64 in iphone application

I ask about this earlier in this forum, but it was closed, I don’t know why, so I am posting my question again. in iphone application I have to upload the image using Base64 encoding, but when I look at the server, the whole image is white (size = 0x0, 54 KO), I am sure that my Base64 code of my drawing is correct, because I have a PHP script, I use it and the image appears fine. here is the php code used to download:

<?php
$filename = "photo_to_upload.jpg";
$handle = fopen($filename, "r");
$imgbinary = fread($handle, filesize($filename));
$data = base64_encode($imgbinary);
fclose($handle);
?>
<img src="./<?php echo $filename ?>" />
<form action="http://host" method="POST">
<input type="hidden" name="data" value="<?php echo $data?>">
<input type="submit" value="envoyer" />
</form>

and Xcode i:

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"photo_to_upload" ofType:@"jpg"];
 NSURL *url = [NSURL fileURLWithPath:filePath]; 
 NSData *imageData = [NSData dataWithContentsOfURL:url];

 NSURLResponse* response;
 NSError* error=nil;

NSMutableData *postData=[[NSMutableData alloc]init];
[postData appendData:[@"data=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[self base64forData:imageData]];


NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL   URLWithString:@"HOST"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];

[[NSURLConnection alloc] initWithRequest:request delegate:self];    

think you need help.

+2
source share
2 answers

I also face the same question Red Poppy Answer Helped me.

Answer that helped

!!! , , blanc "+", "+" blanc, Base64 "+", ( blanc find), "+" base64,

to solve that i replac "+" with ASCII equivalente like this 

:[[self base64forData:imageData]stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"].
+5

, postLength, , ( , ). :

[request
    setValue:[NSString stringWithFormat:@"%u", [postData length]] 
    forHTTPHeaderField:@"Content-Length"
];
0

All Articles