I'm new to mobile apps and developing an APP where I need to upload the photo and store in to the server.
I have the JS code as follows to store photo in to the localstorage first and then get that photo using getitem and convert in to base64 and finally move it to server with image conversion technique (base64_decode) using PHP
function saveImage(photoURI,task_id) {
alert("in save image");
//remove the old photo if exists
var old_photoURI;
if (old_photoURI = loadImage(task_id)) {
deleteImage(old_photoURI,task_id);
}
//update the local database
localStorage.setItem("tmp_photo_"+task_id, photoURI);
return true; }
code to get the image stored on localstorage
function loadImage(task_id) {
alert("in load image")
//read the current photo
return localStorage.getItem("tmp_photo_"+task_id); }
and below code is to convert the image in to base64
var dt = new Date();
var timeupload = dt.getDate() + "" + (dt.getMonth()+1) + "" + dt.getFullYear() + "" + dt.getHours() + "" + dt.getMinutes() + "" + dt.getSeconds();
imgname = task_id+"_"+timeupload+'.jpeg';
alert(loadImage(task_id) + "load image task id");
var filesSelected = loadImage(task_id);
alert(filesSelected + "files selected");
alert(filesSelected.length + "files lenth");
if (filesSelected.length > 0)
{
var fileToLoad = filesSelected[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent) {
var srcData = fileLoadedEvent.target.result; // <--- data: base64
var newImage = document.createElement('img');
newImage.src = srcData;
base64_string = newImage.outerHTML;
alert(srcData + "srouce data now");
}
fileReader.readAsDataURL(fileToLoad);
}
and finally pass this srcData value to Service, which has all functionality to convert into image again and save it into folder on server.
I have save functionality (uploading the signature to server) which works perfectly with the same code.