php - Base64 Over HTTP POST losing data (Objective-C) -
i have http post request , base64 encoding library, encode image b64 send on http via post method.
i output base64 xcodes console, re-create , paste , works perfectly. although base64 store within database (mongodb, plain text file etc) comes out corrupt on other end.
working version (copied , pasted xcode): http://dontpanicrabbit.com/api/working.php broken version (from mongodb database): http://dontpanicrabbit.com/api/grabimage.php
if view source you'll notice same there added whitespace broken version.
the objective-c code using is:
myimage.image = [info objectforkey:uiimagepickercontrolleroriginalimage]; nsdata *imagedata = uiimagejpegrepresentation(myimage.image, 0); [base64 initialize]; nsstring *encoded = [base64 encode:imagedata]; nsstring *urlpost = encoded; //nslog(@"%@",encoded); nsstring *varyingstring1 = @"picture="; nsstring *varyingstring2 = urlpost; nsstring *post = [nsstring stringwithformat: @"%@%@", varyingstring1, varyingstring2]; nslog(@"%@", post); //nsstring *post = @"image=%@",urlpost; nsdata *postdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:yes]; nsstring *postlength = [nsstring stringwithformat:@"%d", [postdata length]]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init]; [request seturl:[nsurl urlwithstring:@"url/api/insertimage.php"]]; [request sethttpmethod:@"post"]; [request sethttpbody:postdata]; nsdata *returndata = [nsurlconnection sendsynchronousrequest: request returningresponse: nil error: nil]; nsstring *strresult = [[nsstring alloc] initwithdata:returndata encoding:nsutf8stringencoding];
php -> mongodb storage
<?php seek { // open connection mongodb server $conn = new mongo('localhost'); // access database $db = $conn->dablia; // access collection $collection = $db->images; // insert new document $item = array( 'picture' => $_post['picture'] ); $collection->insert($item); echo 'inserted document id: ' . $item['_id']; // disconnect server $conn->close(); } grab (mongoconnectionexception $e) { die('error connecting mongodb server'); } grab (mongoexception $e) { die('error: ' . $e->getmessage()); } ?>
output code:
<?php seek { // open connection mongodb server $conn = new mongo('localhost'); // access database $db = $conn->dablia; // access collection $collection = $db->images; // execute query // retrieve documents $cursor = $collection->find(); // iterate through result set // print each document foreach ($cursor $obj) { echo '<img src="data:image/jpeg;base64,'.trim($obj['picture']).'">'; } // disconnect server $conn->close(); } grab (mongoconnectionexception $e) { die('error connecting mongodb server'); } grab (mongoexception $e) { die('error: ' . $e->getmessage()); } ?>
i have no thought why seem corrupting on post?
the problem suggested in first comment. is, base64 encoded info can contain '+' characters. in x-www-form-urlencoded info receiver knows '+' encoding of space character. since aren't url encoding base64 value, instances of '+' cause info corrupted when received.
the '+' characters in initial info turning ' ' when received , stored. when output value, invalid base64 encoded data.
if examine source of working vs. non-working examples you'll see whitespace exists there '+' in original base64 encoded value. newlines you're seeing because whatever you're viewing source in wrapping lines @ ' ' character.
in ios code need encode base64 encoded value, in case need percent encode '+' characters.
edit add, in response comment:
post = [post stringbyreplacingoccurrencesofstring:@"+" withstring:@"%2b"];
php ios objective-c xcode http
No comments:
Post a Comment