I have been attempting to upload images to Firebase storage using the cordova-plugin-camera but have not been successful:
Below is the code I have been using:
let options:any = {
quality : 100,
destinationType : Camera.DestinationType.DATA_URL,
sourceType : Camera.PictureSourceType.CAMERA,
allowEdit : false,
encodingType: Camera.EncodingType.JPEG,
mediaType: Camera.MediaType.PICTURE,
targetWidth: 1920,
targetHeight: 1920,
saveToPhotoAlbum: true,
cameraDirection: Camera.Direction.FRONT
};
Camera.getPicture(this.options).then( (imageData) => {
let blob: any = new Blob( [imageData], { type: "image/jpeg" } );
blob.name = 'image.jpg';
let storageRef: any = firebase.storage().ref();
let path: string = 'images/' + this.user.uid + '/' + Math.random().toString(36).substr(2, 9) + '.jpg';
console.log(path);
let uploadTask: any = storageRef.child(path).put(blob);
uploadTask.on('state_changed', function(snapshot) {
console.log(snapshot);
}, function(error) {
this.showAlert(error);
}, function() {
var downloadURL = uploadTask.snapshot.downloadURL;
console.log(downloadURL);
console.log(this.user);
this.user.set({photo: downloadURL});
});
}, (err) => {
this.showAlert(err);
});
For some reason, the file seems to upload but when I check, it is empty.
Any assistance would be greatly appreciated.
Thank you.