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 => {
});
})
}
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.
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.
StorageReference. getParent() Returns a new instance of StorageReference pointing to the parent location or null if this instance references the root location. String. getPath()
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.
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 => {
});
})
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With