-3

Read my older post first and read my posted answer first to understand my current situation better Link.

Dose any body know of any JavaScript methods to escape sensitive characters of a encoded base 64 string in a JSON object to be read correctly in the server side?

Recently I could not get my encoded base 64 string from a JSON object to be read correctly in the server side. So I used a JavaScript method call encodeURIComponent() and that is used to escape special characters of a URL

string. So I used it on my encoded base 64 string from my JSON object from the client side and it worked surprisingly but this resolved method has limits since encodeURIComponent() is used for URL strings it can not work on really really long encoded base 64 strings. I tested this on a encoded base 64 string of a large video and it gave me this error

Uncaught RangeError: Invalid string length

I know that method is used for URLs but it made it work and I realized I have to find another way to escape my encoded base 64 string from my JSON object from the client side to be able to work correctly in the server side so do any of you guys know any other JavaScript methods that can escape the encoded base 64 string from the client side to be read properly in the server side?

This is my code

index.php

<style>

#photo-input{
display: block;
margin-bottom: 50px;
}

</style>

<script>

document.addEventListener('DOMContentLoaded',function(){

document.querySelector('#submit').addEventListener('click',function(){

var photo_input= document.querySelector('#photo-input').files[0];

//Convert #photo-input content into a base 64 string
var reader = new FileReader();
reader.readAsDataURL(photo_input);

reader.onload = function (){
var photo_input_result= reader.result;
sendUploadInfo(photo_input_result);
}
//

});

function sendUploadInfo(photo_input_result){

//Remove the data:image/file_extension;base64, prefix by not removing this you wont be able to view this as a file in any computing application
var remove_the_photo_file_reader_prefix= photo_input_result.split(',')[1];
//

//Escape incompatible characters that won't work properly in JSON and PHP
var escape_incompatible_characters_from_the_photo= encodeURIComponent(remove_the_photo_file_reader_prefix);
//

var photo= escape_incompatible_characters_from_the_photo;

//<JSON data>

var upload_info = {
    first_name: "John",
    last_name: "Smith",
    photo: photo
};

//</JSON data>

var upload_info_json_object= 'upload_info_json_object='+JSON.stringify(upload_info); 

//<AJAX>
var xhr= new XMLHttpRequest();
xhr.onreadystatechange= function(){

if(xhr.readyState == 4){

document.querySelector('#output').innerHTML= xhr.responseText;

}
}

xhr.open('POST','x');
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(upload_info_json_object);
//</AJAX>
}

});

</script>

<input type='file' id='photo-input'>

<button id='submit'>Send JSON data</button>

<div id='output'></div>

x.php

<?php

$upload_info_json_object = json_decode($_POST['upload_info_json_object']);

$first_name= $upload_info_json_object->first_name;
$last_name= $upload_info_json_object->last_name;


//Photo upload section
$photo= $upload_info_json_object->photo;

//Decode into a file
$photo= base64_decode($photo); 

file_put_contents('geeksforgeeks-22.jpg',$photo);

//

?>

<h1><?php echo $first_name.' '.$last_name.' just uploaded a photo.'; ?></h1>

One more thing

Please don't comment criticizing statements about how bad it is to send a large file that is encoded in a base 64 string to the server because if you do I will just ignore your comments and I am aware of other easier methods like the HTML

form tag method and JavaScript's FormData() method so please don't suggest those methods or other methods that is not base 64 related because for personal reasons I want know how I can do this in base 64 regardless how inferior this method is when compared to other methods I'm just saying.

  • `escape sensitive characters of a encoded base 64 string` so, you mean escape the `+`, `/` and `=`? – Jaromanda X Jul 14 '19 at 23:34
  • I believe those might be the suspects of why it can not be read properly in the server side. –  Jul 14 '19 at 23:39
  • I can't see A-Z, a-z or 0-9 being an issue, so, look up Url64 in your favourite search engine – Jaromanda X Jul 14 '19 at 23:39
  • I can test that out what method can I use to escape those suspected characters that you know of and ok I will search about that on google. –  Jul 14 '19 at 23:40
  • It's not enough to just worry about the non-`[A-Za-z0-9]` characters used for base64 encoding, there will also be curly braces, commas, colons, and possibly backslashes used by JSON that have to be encoded as well. – kshetline Jul 14 '19 at 23:44
  • @charlietfl, there's meta data being sent along with the base64-encoded image (first name and last name), so I guess that's why JSON is being used, to bundle that meta data with the image. – kshetline Jul 14 '19 at 23:47
  • @JaromandaX I google searched url64 and no idea how I can use that info to fix my problem and i'm not focusing on urls I hope I did not confused you when I added the the encodeURIComponent method I only used that because I can not use the escape() method so you know any other solutions ? –  Jul 15 '19 at 00:04

1 Answers1

1

Can't you just do this:

xhr.setRequestHeader("Content-type", "application/json");

And send the JSON as JSON, instead of URL-encoding it?

(Edited to add some more info for the receiving side)

According to this post here: How to get body of a POST in php?

You can use this:

$entityBody = file_get_contents('php://input');

...or this:

$entityBody = stream_get_contents(STDIN);

To read the whole posted content without worrying about having to have a variable name encoded to refer to the content.

kshetline
  • 12,547
  • 4
  • 37
  • 73
  • I tried out the xhr.setRequestHeader("Content-type", "application/json"); advice it gave me a few errors in the server side for example the Undefined index: upload_info_json_object and the photo and $first_name and $last_name variables can no longer be read so I know now I have to stick to xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); and I'm ma try your other suggestion about the $entityBody = stream_get_contents(STDIN); suggestion give me a moment. –  Jul 14 '19 at 23:48
  • Just so you know if you're trying the `$entityBody = stream...` part, that goes hand-in-hand with the first part of my suggestion. If you try that alone, but stick with encoding the info the way you did in the first place, that's not going to work. – kshetline Jul 14 '19 at 23:50
  • I never meant the first part to work with the way you'd already written the receiving side, by the way. I added the second part because I realized you might not know how to get the entity body and NOT use something like `$_POST['upload_info_json_object']`. – kshetline Jul 14 '19 at 23:52
  • So don't use $_POST['upload_info_json_object'] is that where you were suggesting ? If not how would I get the other JSON object values example first name last name –  Jul 15 '19 at 00:14