javascript post base64 encrypt and decrypt image












0















I am working with javascript image post in base64.
When the image is posted it showsinvalid image all the time. How to solve this?



I can't understand what's the problem.






function encodeImageFileAsURL(element) {
var file = element.files[0];
var reader = new FileReader();
reader.onloadend = function() {

var xxhttp = new XMLHttpRequest();
xxhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {

document.getElementById('output').src = "data:image/png;base64,//" + this.responseText + "";




}
};
xxhttp.open("POST", "itemimages.php", true);
xxhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xxhttp.send('image_data=' + reader.result);

}
reader.readAsDataURL(file);
}

<input type="file" accept="image/png" onchange="encodeImageFileAsURL(this)" />
<img id="output" src="" alt="" />





My php code in "itemimages.php" file



$imagedata = $_POST['image_data'];

$imagepath = "images/aa.png";

$imagedata = preg_replace( '/data:image/.*;base64,/', '', $imagedata );
if(file_put_contents($imagepath,base64_decode($imagedata)))
{
echo "$imagedata";
}


It contains an encrypted string but the string is not showing an image










share|improve this question

























  • You should probably be using a content type of form data and not form-url encoded.

    – basic
    Jan 3 at 16:57











  • I already try with this ` xxhttp.setRequestHeader("Content-type","multipart/form-data");` but it take blank data.

    – Tamal Dev
    Jan 3 at 17:19













  • Note that you add data:image/png;base64,// to the data before sending, but only remove data:image/png;base64, in your preg_replace() call. This could be resulting in an invalid encoding. Try changing the regular expression to /data:image/.*;base64,/// instead and see if that helps.

    – B. Fleming
    Jan 3 at 17:21
















0















I am working with javascript image post in base64.
When the image is posted it showsinvalid image all the time. How to solve this?



I can't understand what's the problem.






function encodeImageFileAsURL(element) {
var file = element.files[0];
var reader = new FileReader();
reader.onloadend = function() {

var xxhttp = new XMLHttpRequest();
xxhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {

document.getElementById('output').src = "data:image/png;base64,//" + this.responseText + "";




}
};
xxhttp.open("POST", "itemimages.php", true);
xxhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xxhttp.send('image_data=' + reader.result);

}
reader.readAsDataURL(file);
}

<input type="file" accept="image/png" onchange="encodeImageFileAsURL(this)" />
<img id="output" src="" alt="" />





My php code in "itemimages.php" file



$imagedata = $_POST['image_data'];

$imagepath = "images/aa.png";

$imagedata = preg_replace( '/data:image/.*;base64,/', '', $imagedata );
if(file_put_contents($imagepath,base64_decode($imagedata)))
{
echo "$imagedata";
}


It contains an encrypted string but the string is not showing an image










share|improve this question

























  • You should probably be using a content type of form data and not form-url encoded.

    – basic
    Jan 3 at 16:57











  • I already try with this ` xxhttp.setRequestHeader("Content-type","multipart/form-data");` but it take blank data.

    – Tamal Dev
    Jan 3 at 17:19













  • Note that you add data:image/png;base64,// to the data before sending, but only remove data:image/png;base64, in your preg_replace() call. This could be resulting in an invalid encoding. Try changing the regular expression to /data:image/.*;base64,/// instead and see if that helps.

    – B. Fleming
    Jan 3 at 17:21














0












0








0








I am working with javascript image post in base64.
When the image is posted it showsinvalid image all the time. How to solve this?



I can't understand what's the problem.






function encodeImageFileAsURL(element) {
var file = element.files[0];
var reader = new FileReader();
reader.onloadend = function() {

var xxhttp = new XMLHttpRequest();
xxhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {

document.getElementById('output').src = "data:image/png;base64,//" + this.responseText + "";




}
};
xxhttp.open("POST", "itemimages.php", true);
xxhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xxhttp.send('image_data=' + reader.result);

}
reader.readAsDataURL(file);
}

<input type="file" accept="image/png" onchange="encodeImageFileAsURL(this)" />
<img id="output" src="" alt="" />





My php code in "itemimages.php" file



$imagedata = $_POST['image_data'];

$imagepath = "images/aa.png";

$imagedata = preg_replace( '/data:image/.*;base64,/', '', $imagedata );
if(file_put_contents($imagepath,base64_decode($imagedata)))
{
echo "$imagedata";
}


It contains an encrypted string but the string is not showing an image










share|improve this question
















I am working with javascript image post in base64.
When the image is posted it showsinvalid image all the time. How to solve this?



I can't understand what's the problem.






function encodeImageFileAsURL(element) {
var file = element.files[0];
var reader = new FileReader();
reader.onloadend = function() {

var xxhttp = new XMLHttpRequest();
xxhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {

document.getElementById('output').src = "data:image/png;base64,//" + this.responseText + "";




}
};
xxhttp.open("POST", "itemimages.php", true);
xxhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xxhttp.send('image_data=' + reader.result);

}
reader.readAsDataURL(file);
}

<input type="file" accept="image/png" onchange="encodeImageFileAsURL(this)" />
<img id="output" src="" alt="" />





My php code in "itemimages.php" file



$imagedata = $_POST['image_data'];

$imagepath = "images/aa.png";

$imagedata = preg_replace( '/data:image/.*;base64,/', '', $imagedata );
if(file_put_contents($imagepath,base64_decode($imagedata)))
{
echo "$imagedata";
}


It contains an encrypted string but the string is not showing an image






function encodeImageFileAsURL(element) {
var file = element.files[0];
var reader = new FileReader();
reader.onloadend = function() {

var xxhttp = new XMLHttpRequest();
xxhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {

document.getElementById('output').src = "data:image/png;base64,//" + this.responseText + "";




}
};
xxhttp.open("POST", "itemimages.php", true);
xxhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xxhttp.send('image_data=' + reader.result);

}
reader.readAsDataURL(file);
}

<input type="file" accept="image/png" onchange="encodeImageFileAsURL(this)" />
<img id="output" src="" alt="" />





function encodeImageFileAsURL(element) {
var file = element.files[0];
var reader = new FileReader();
reader.onloadend = function() {

var xxhttp = new XMLHttpRequest();
xxhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {

document.getElementById('output').src = "data:image/png;base64,//" + this.responseText + "";




}
};
xxhttp.open("POST", "itemimages.php", true);
xxhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xxhttp.send('image_data=' + reader.result);

}
reader.readAsDataURL(file);
}

<input type="file" accept="image/png" onchange="encodeImageFileAsURL(this)" />
<img id="output" src="" alt="" />






javascript php html base64






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 17:24









Stundji

497314




497314










asked Jan 3 at 16:54









Tamal DevTamal Dev

105




105













  • You should probably be using a content type of form data and not form-url encoded.

    – basic
    Jan 3 at 16:57











  • I already try with this ` xxhttp.setRequestHeader("Content-type","multipart/form-data");` but it take blank data.

    – Tamal Dev
    Jan 3 at 17:19













  • Note that you add data:image/png;base64,// to the data before sending, but only remove data:image/png;base64, in your preg_replace() call. This could be resulting in an invalid encoding. Try changing the regular expression to /data:image/.*;base64,/// instead and see if that helps.

    – B. Fleming
    Jan 3 at 17:21



















  • You should probably be using a content type of form data and not form-url encoded.

    – basic
    Jan 3 at 16:57











  • I already try with this ` xxhttp.setRequestHeader("Content-type","multipart/form-data");` but it take blank data.

    – Tamal Dev
    Jan 3 at 17:19













  • Note that you add data:image/png;base64,// to the data before sending, but only remove data:image/png;base64, in your preg_replace() call. This could be resulting in an invalid encoding. Try changing the regular expression to /data:image/.*;base64,/// instead and see if that helps.

    – B. Fleming
    Jan 3 at 17:21

















You should probably be using a content type of form data and not form-url encoded.

– basic
Jan 3 at 16:57





You should probably be using a content type of form data and not form-url encoded.

– basic
Jan 3 at 16:57













I already try with this ` xxhttp.setRequestHeader("Content-type","multipart/form-data");` but it take blank data.

– Tamal Dev
Jan 3 at 17:19







I already try with this ` xxhttp.setRequestHeader("Content-type","multipart/form-data");` but it take blank data.

– Tamal Dev
Jan 3 at 17:19















Note that you add data:image/png;base64,// to the data before sending, but only remove data:image/png;base64, in your preg_replace() call. This could be resulting in an invalid encoding. Try changing the regular expression to /data:image/.*;base64,/// instead and see if that helps.

– B. Fleming
Jan 3 at 17:21





Note that you add data:image/png;base64,// to the data before sending, but only remove data:image/png;base64, in your preg_replace() call. This could be resulting in an invalid encoding. Try changing the regular expression to /data:image/.*;base64,/// instead and see if that helps.

– B. Fleming
Jan 3 at 17:21












1 Answer
1






active

oldest

votes


















0














If your data arrives correctly to php ... you should try this



$imagedata = $_POST['image_data'];

$image_parts = explode(";base64,", $imagedata);
$image_type_aux = explode("image/", $image_parts[0]);
$image_base64 = base64_decode($image_parts[1]);

$imagepath = "images/aa." . $image_type_aux[1];

file_put_contents($imagepath , $image_base64);


--- javascript ---



function encodeImageFileAsURL(element) {
var file = element.files[0];
var reader = new FileReader();
reader.onloadend = function() {
console.log(reader.result)
}
reader.readAsDataURL(file);
}


-- html --



<input type="file" onchange="encodeImageFileAsURL(this)">





share|improve this answer


























  • sorry! Bro it's doesn't work too.

    – Tamal Dev
    Jan 3 at 17:31











  • use the other function to encode image

    – Diego Salguero
    Jan 3 at 17:38











  • javascript function are same. Your function just output on the console.

    – Tamal Dev
    Jan 4 at 2:12












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026564%2fjavascript-post-base64-encrypt-and-decrypt-image%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














If your data arrives correctly to php ... you should try this



$imagedata = $_POST['image_data'];

$image_parts = explode(";base64,", $imagedata);
$image_type_aux = explode("image/", $image_parts[0]);
$image_base64 = base64_decode($image_parts[1]);

$imagepath = "images/aa." . $image_type_aux[1];

file_put_contents($imagepath , $image_base64);


--- javascript ---



function encodeImageFileAsURL(element) {
var file = element.files[0];
var reader = new FileReader();
reader.onloadend = function() {
console.log(reader.result)
}
reader.readAsDataURL(file);
}


-- html --



<input type="file" onchange="encodeImageFileAsURL(this)">





share|improve this answer


























  • sorry! Bro it's doesn't work too.

    – Tamal Dev
    Jan 3 at 17:31











  • use the other function to encode image

    – Diego Salguero
    Jan 3 at 17:38











  • javascript function are same. Your function just output on the console.

    – Tamal Dev
    Jan 4 at 2:12
















0














If your data arrives correctly to php ... you should try this



$imagedata = $_POST['image_data'];

$image_parts = explode(";base64,", $imagedata);
$image_type_aux = explode("image/", $image_parts[0]);
$image_base64 = base64_decode($image_parts[1]);

$imagepath = "images/aa." . $image_type_aux[1];

file_put_contents($imagepath , $image_base64);


--- javascript ---



function encodeImageFileAsURL(element) {
var file = element.files[0];
var reader = new FileReader();
reader.onloadend = function() {
console.log(reader.result)
}
reader.readAsDataURL(file);
}


-- html --



<input type="file" onchange="encodeImageFileAsURL(this)">





share|improve this answer


























  • sorry! Bro it's doesn't work too.

    – Tamal Dev
    Jan 3 at 17:31











  • use the other function to encode image

    – Diego Salguero
    Jan 3 at 17:38











  • javascript function are same. Your function just output on the console.

    – Tamal Dev
    Jan 4 at 2:12














0












0








0







If your data arrives correctly to php ... you should try this



$imagedata = $_POST['image_data'];

$image_parts = explode(";base64,", $imagedata);
$image_type_aux = explode("image/", $image_parts[0]);
$image_base64 = base64_decode($image_parts[1]);

$imagepath = "images/aa." . $image_type_aux[1];

file_put_contents($imagepath , $image_base64);


--- javascript ---



function encodeImageFileAsURL(element) {
var file = element.files[0];
var reader = new FileReader();
reader.onloadend = function() {
console.log(reader.result)
}
reader.readAsDataURL(file);
}


-- html --



<input type="file" onchange="encodeImageFileAsURL(this)">





share|improve this answer















If your data arrives correctly to php ... you should try this



$imagedata = $_POST['image_data'];

$image_parts = explode(";base64,", $imagedata);
$image_type_aux = explode("image/", $image_parts[0]);
$image_base64 = base64_decode($image_parts[1]);

$imagepath = "images/aa." . $image_type_aux[1];

file_put_contents($imagepath , $image_base64);


--- javascript ---



function encodeImageFileAsURL(element) {
var file = element.files[0];
var reader = new FileReader();
reader.onloadend = function() {
console.log(reader.result)
}
reader.readAsDataURL(file);
}


-- html --



<input type="file" onchange="encodeImageFileAsURL(this)">






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 17:37

























answered Jan 3 at 17:22









Diego SalgueroDiego Salguero

12014




12014













  • sorry! Bro it's doesn't work too.

    – Tamal Dev
    Jan 3 at 17:31











  • use the other function to encode image

    – Diego Salguero
    Jan 3 at 17:38











  • javascript function are same. Your function just output on the console.

    – Tamal Dev
    Jan 4 at 2:12



















  • sorry! Bro it's doesn't work too.

    – Tamal Dev
    Jan 3 at 17:31











  • use the other function to encode image

    – Diego Salguero
    Jan 3 at 17:38











  • javascript function are same. Your function just output on the console.

    – Tamal Dev
    Jan 4 at 2:12

















sorry! Bro it's doesn't work too.

– Tamal Dev
Jan 3 at 17:31





sorry! Bro it's doesn't work too.

– Tamal Dev
Jan 3 at 17:31













use the other function to encode image

– Diego Salguero
Jan 3 at 17:38





use the other function to encode image

– Diego Salguero
Jan 3 at 17:38













javascript function are same. Your function just output on the console.

– Tamal Dev
Jan 4 at 2:12





javascript function are same. Your function just output on the console.

– Tamal Dev
Jan 4 at 2:12




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026564%2fjavascript-post-base64-encrypt-and-decrypt-image%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Angular Downloading a file using contenturl with Basic Authentication

Olmecas

Can't read property showImagePicker of undefined in react native iOS