Como subir una imagen desde cordova via ajax
/**
* Función para pasar una imagen formato url a BLOB (objeto)
* @param {string} pathImage
*/
var loadImage = function(pathImage) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open("GET", pathImage, true);
xhr.responseType = "blob";
xhr.onload = function (e) {
// console.log(this.response);
// var reader = new FileReader();
// reader.onload = function(event) {
// var res = event.target.result;
// // console.log(res)
// };
// var file = this.response;
// reader.readAsDataURL(file);
resolve(this.response);
};
xhr.send();
});
};
$('.take-foto').on('click',function(){
var options = {
encodingType: 0,
quality: 60,
destinationType: Camera.DestinationType.FILE_URI,
correctOrientation: true,
targetWidth: 4000,
targetHeight: 3000
};
navigator.camera.getPicture(savePicture, onFailCamara, options);
})
Como utilizar la función
function savePicture (imageURI){
// Si queremos mostrar la imagen
var img = $('<div class="image"><img src="'+imageURI+'"></div>');
loadImage(imageURI).then(function(imageBlob){
var dataSend = new FormData();
dataSend.append("upload",1);
dataSend.append("photo", imageBlob);
$.ajax({
method: "POST",
cache: false,
contentType: false,
processData: false,
url: _url_,
data: dataSend,
})
.done(function(data) {
console.log(data);
})
.fail(function( jqxhr, textStatus, error ) {
console.log(error)
});
},function(){
console.log('error');
});
}