convert a base64 image into a image file and upload it with an asynchronous form using jQuery
convert a base64 image into a image file and upload it with an asynchronous form using jQuery
by M Shameer
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Base64 string to a file in form</title>
</head>
<body>
<form id="myAwesomeForm" method="post" action="endpoint.php">
<input type="text" id="filename" name="filename" /> <!-- Filename -->
<input type="submit" id="submitButton" name="submitButton" /> <!-- Submit -->
</form>
<script>
function b64toBlob(b64Data, contentType, sliceSize) {
contentType = contentType || '';
sliceSize = sliceSize || 512;
var byteCharacters = atob(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {type: contentType});
return blob;
}
$("#myAwesomeForm").submit(function(e){
e.preventDefault();
appendFileAndSubmit();
});
function appendFileAndSubmit(){
// Get the form
var form = document.getElementById("myAwesomeForm");
var ImageURL =...