Note that there are some explanatory texts on larger screens.

plurals
  1. POBase64 Over HTTP POST losing data (Objective-C)
    text
    copied!<p>I currently have a HTTP POST Request and a Base64 Encoding Library, I encode my image to B64 then send it over HTTP via the POST method.</p> <p>I output the Base64 to XCodes console, copy and paste it and it works perfectly. Although the Base64 I store within the Database (MongoDB, Plain Text File etc) always comes out corrupt on the other end.</p> <p>Working Version (Copied and Pasted from XCode): <a href="http://dontpanicrabbit.com/api/working.php">http://dontpanicrabbit.com/api/working.php</a> Broken Version (From MongoDB Database): <a href="http://dontpanicrabbit.com/api/grabimage.php">http://dontpanicrabbit.com/api/grabimage.php</a></p> <p>If you view the source you'll notice they are the same but there is added whitespace into the broken version.</p> <p>The Objective-C code I am using is:</p> <pre><code>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]; </code></pre> <p>PHP -> MongoDB Storage</p> <pre><code>&lt;?php try { // open connection to MongoDB server $conn = new Mongo('localhost'); // access database $db = $conn-&gt;dablia; // access collection $collection = $db-&gt;images; // insert a new document $item = array( 'picture' =&gt; $_POST['picture'] ); $collection-&gt;insert($item); echo 'Inserted document with ID: ' . $item['_id']; // disconnect from server $conn-&gt;close(); } catch (MongoConnectionException $e) { die('Error connecting to MongoDB server'); } catch (MongoException $e) { die('Error: ' . $e-&gt;getMessage()); } ?&gt; </code></pre> <p>Output Code:</p> <pre><code>&lt;?php try { // open connection to MongoDB server $conn = new Mongo('localhost'); // access database $db = $conn-&gt;dablia; // access collection $collection = $db-&gt;images; // execute query // retrieve all documents $cursor = $collection-&gt;find(); // iterate through the result set // print each document foreach ($cursor as $obj) { echo '&lt;img src="data:image/jpeg;base64,'.trim($obj['picture']).'"&gt;'; } // disconnect from server $conn-&gt;close(); } catch (MongoConnectionException $e) { die('Error connecting to MongoDB server'); } catch (MongoException $e) { die('Error: ' . $e-&gt;getMessage()); } ?&gt; </code></pre> <p>I have no idea why I seem to be corrupting over POST?</p>
 

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