Created
August 18, 2020 23:11
-
-
Save ahrherrera/adfb187c967bc1c9740e58e7908e237f to your computer and use it in GitHub Desktop.
Para Subir imagenes a Firebase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const options : CameraOptions = { | |
quality: 50, // picture quality | |
destinationType: this.camera.DestinationType.DATA_URL, | |
encodingType: this.camera.EncodingType.JPEG, | |
mediaType: this.camera.MediaType.PICTURE , | |
correctOrientation: true | |
} | |
this.camera.getPicture(options) .then((imageData) => { | |
let imagen = { | |
titulo: 'imagen deposito', | |
img: imageData | |
} | |
this.cargar.cargarImagenes(imagen).then( url => { | |
//obtenemos la url from firebase y la subiremos a el backend | |
// ahora preparamos objeto para ingresar el envio | |
this.id_envios = []; | |
this.data.forEach(x => { | |
this.id_envios.push(x.id); | |
}); | |
let depo = { | |
"boucher": {"remot": url} , | |
"monto" : this.deposito_pendiente , | |
"afiliado": this.auth.userdata.afiliado.id, | |
"tipo": "Boucher" | |
} | |
this.auth.GuardarDeposito(depo).subscribe(resp => { // Enviar la URL y la info al Backend | |
console.log(resp); | |
this.navCtrl.pop(); | |
} , error2 => { | |
console.log(error2); | |
this.util.presentAlert('Error' , 'ha ocurrido un error ingresando el deposito'); | |
}); | |
} , error => { | |
console.log(error); | |
}); | |
}, (err) => { | |
console.log(err); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cargarImagenes ( imagen) { | |
let promesa = new Promise ( (resolve , reject) => { | |
this.utils.presentToast('cargando imagenes...'); | |
// referencia a base de datos firebase | |
let storeRef = firebase.storage().ref(); | |
// nombre de la imagen a partir de una fecha en string | |
let nombreImagen: string = new Date().valueOf().toString(); | |
// ahora la funcion para subir imagenes a firebase | |
console.log('imagen.img', imagen.img); | |
let subiriamgenes: firebase.storage.UploadTask = storeRef.child(`img/${nombreImagen}`).putString( imagen.img , 'base64' , {contentType: 'image/jpeg' } ); | |
//ahora a chekear la subida de imagenes | |
subiriamgenes.on(firebase.storage.TaskEvent.STATE_CHANGED, | |
()=>{} , // saber cuantos mbs han sido subidos | |
( error )=> { | |
// manejo de errores | |
console.log('error en la carga de imagenes ' , JSON.stringify(error)); | |
this.utils.presentToast(JSON.stringify(error)); | |
reject(); | |
} , | |
()=> { | |
// todo sucedio corectamente | |
this.utils.presentToast('imagen subida correctamente'); | |
// obtendremos el url de la imagen | |
let url = subiriamgenes.snapshot.ref.getDownloadURL(); // Esta es la URL para acceder a la imagen y enviarla al backend | |
resolve(url); | |
} | |
); | |
}); | |
return promesa; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment