Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 => {
      });
    })
  }
like image 522
Stevie Star Avatar asked Jan 30 '17 14:01

Stevie Star


People also ask

How do I get metadata from Firebase Storage?

Get File Metadata This metadata can be retrieved from a Cloud Storage reference using the getMetadata() method. getMetadata() returns a Promise containing the complete metadata, or an error if the Promise rejects.

Does Firebase Storage have CDN?

Firebase Hosting uses a powerful global CDN to make your site as fast as possible. Any requested static content is automatically cached on the CDN. If you redeploy your site's content, Firebase Hosting automatically clears all your cached static content across the CDN until the next request.

What is Storage reference in Firebase?

StorageReference. getParent() Returns a new instance of StorageReference pointing to the parent location or null if this instance references the root location. String. getPath()

Can Firebase be used to store data?

Firebase Realtime Database is a NoSQL cloud database that is used to store and sync the data. The data from the database can be synced at a time across all the clients such as android, web as well as IOS. The data in the database is stored in the JSON format and it updates in real-time with every connected client.


1 Answers

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 => {
      });
    })
  }
like image 93
Stevie Star Avatar answered Oct 10 '22 13:10

Stevie Star