Angular 2 - Firebase Storage -putString() 方法支持元数据? [英] Angular 2 - Firebase Storage -putString() method support metadata?

查看:17
本文介绍了Angular 2 - Firebase Storage -putString() 方法支持元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ionic 2 应用程序中有一个简单的功能,可以将文件上传到我的 firebase 存储服务器.它从相机中抓取图像的 base64 编码字符串,但是当我不尝试强制 content-type 时,它默认为 application/octet-stream.当我尝试将元数据添加到 putString() 方法时,出现错误.

I have a simple function in my ionic 2 app to upload a file to my firebase storage server. It grabs a base64 encoded string of an image from a Camera, but when I don't try to force the content-type, it defaults to application/octet-stream. When I try to add the metadata to the putString() method, I get errors.

有谁知道我如何使用 putString 做到这一点?

Does anyone know how I can do this with putString?

这是我目前的功能:

uploadProfilePhoto(file) {
    this.storage.get('user').then(user => {
      let id = user.id;

      var metadata = {
        contentType: 'image/jpeg',
      };

      let userProfileRef = this.fbStorage.ref(`/users/${id}/profile_photo/profile_photo.jpg`);
      userProfileRef.putString(file, metadata).then(snapshot => {
      }).catch(error => {
      });
    })
  }

推荐答案

因此,我缺少指定 base64 的参数.这是更新后的功能:

So with this, I was missing a parameter to specify base64. Here is the updated function:

uploadProfilePhoto(file) {
    this.storage.get('user').then(user => {
      let id = user.id;

      var metadata = {
        contentType: 'image/jpeg',
      };

      let userProfileRef = this.fbStorage.ref(`/users/${id}/profile_photo/profile_photo.jpg`);
      userProfileRef.putString(file, 'base64', metadata).then(snapshot => {
      }).catch(error => {
      });
    })
  }

这篇关于Angular 2 - Firebase Storage -putString() 方法支持元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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