如何将文件放在firebase存储中的离子 [英] How to put file on firebase storage in ionic

查看:203
本文介绍了如何将文件放在firebase存储中的离子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cordovaCamera插件在我的离子应用程序中获取文件URL。

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

我希望将此文件放在firebase存储而不是base64

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

这是我的参考代码

$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")
});


推荐答案

为了使其有效,您需要使用 destinationType:Camera.DestinationType.FILE_URI 获取图片的网址。

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

然后,您可以获得Blob来自此图像的 cordova-plugin-file (不要忘记添加它)并将其发送到Firebase。

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

此代码可能有语法错误我将其从Ionic 2代码转换。

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")
    });

这篇关于如何将文件放在firebase存储中的离子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆