【问题标题】:How to put file on firebase storage in ionic如何将文件放在离子的firebase存储中
【发布时间】:2018-03-28 23:18:33
【问题描述】:

我正在使用 cordovaCamera 插件在我的 ionic 应用程序中获取文件 url。

我希望将此文件放在 Firebase 存储中不像 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")
});

【问题讨论】:

  • "imageData" 看起来是最合乎逻辑的。它不工作吗?
  • imageData 返回文件的路径。我需要放置 Blob 或文件对象

标签: javascript cordova firebase ionic-framework firebase-storage


【解决方案1】:

为了使其正常工作,您需要使用destinationType: Camera.DestinationType.FILE_URI 来获取图像的 URL。

然后,您可以使用 cordova-plugin-file 从此图像中获取 Blob(不要忘记添加它)并将其发送到 Firebase。

这段代码可能有语法错误,我是从 Ionic 2 代码转换过来的。

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

【讨论】:

  • 我正在 ionic1 上开发。此外,cordova-plugin-file 无法解析 IOS 中的路径。我不能使用这个插件。我正在使用destinationType FILE_URI
猜你喜欢
  • 2021-11-15
  • 2023-03-17
  • 2018-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-02
  • 2019-03-02
  • 1970-01-01
相关资源
最近更新 更多