Angular 2 - Firebase Storage -putString() method support metadata?
Asked Answered
C

2

10

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.

Does anyone know how I can do this with putString?

Here is my current 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, metadata).then(snapshot => {
      }).catch(error => {
      });
    })
  }
Casaleggio answered 30/1, 2017 at 14:58 Comment(0)
C
11

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 => {
      });
    })
  }
Casaleggio answered 30/1, 2017 at 16:19 Comment(0)
K
0

In addition, you should add custom meta data values like so:

var metadata = {
    contentType: 'image/jpeg',
    customMetadata: {
        foo: "bar"
    }
};

for it to be stored correctly enter image description here

Kimberliekimberlin answered 29/12, 2023 at 9:59 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.