How to put file on firebase storage in ionic

你说的曾经没有我的故事 提交于 2019-12-23 05:36:13

问题


I am using cordovaCamera plugin to get file url in my ionic app.

I wish to put this file on firebase storage not as base64 .

Here is my code for reference

$cordovaCamera.getPicture(options)
.then(function(imageData) { 
var pthref = firebase.storage().ref().child("a/b");
pthref.put(WHAAT SHOULD GO HERE).then(function(snapshot) {
alert('file uploaded!');
},
function(a)
{
alert("error"+JSON.stringify(a))});
},
function(err) {
alert("Camera Error")
});

回答1:


In order to make it works you will need to use destinationType: Camera.DestinationType.FILE_URI to get the URL of the image.

Then, you can get the Blob from this image with cordova-plugin-file (don't forget to add it) and send it to Firebase.

This code could have syntax mistake I am converting it from Ionic 2 code.

var options = {
    destinationType: Camera.DestinationType.FILE_URI
};
$cordovaCamera.getPicture(options)
    .then(function (fileUrl) {
        window.resolveLocalFileSystemURL(fileUrl, (fileEntry) => {
            fileEntry.file((file) => {
                let reader = new FileReader();
                reader.onloadend = function () {
                let imgBlob = new Blob([ this.result ], { type: "image/jpeg" } );
                // Send file to firebase here
                var pthref = firebase.storage().ref().child("a/b");
                pthref.put(imgBlob);
                };
                reader.onerror = (error) => {
                console.error(error);
                };
                reader.readAsArrayBuffer(file);
            }, (error) => {
                console.error(error);
            });
        })
    },
    function (err) {
        alert("Camera Error")
    });


来源:https://stackoverflow.com/questions/46787197/how-to-put-file-on-firebase-storage-in-ionic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!