I am trying to upload an image to Firebase storage and save some specific metadata to Cloud Firebase. I am coding in JavaScript.
The goal is to set also configured metadata in Firebase Cloud, for example, from a text input field that a user must fill out.
The way I store images in Firebase storage:
storageRef.child('images/' + file.name).put(file, metadata).then(function(snapshot) { console.log('Uploaded', snapshot.totalBytes, 'bytes.'); console.log(snapshot.metadata); var url = snapshot.downloadURL; console.log('File available at', url); // [START_EXCLUDE] document.getElementById('linkbox').innerHTML = '<a href="' + url + '">Click For File</a>'; // [END_EXCLUDE] }).catch(function(error) { // [START onfailure] console.error('Upload failed:', error); // [END onfailure] }); // [END oncomplete] }
I have no idea how to integrate another task into the upload function to write metadata to Firebase Cloud. Any help would be appreciated!
@eykjs @Sam Storie: Thanks for all your help. I changed my code. Right now, there is a mistake that I cannot understand what happened. Error: TypeError: undefined is not an object (evaluation "selectedFile.name")
My code is:
var selectedFile; function handleFileSelect(event) { //$(".upload-group").show(); selectedFile = event.target.files[0]; }; function confirmUpload() { var metadata = { contentType: 'image', customMetadata: { 'dogType': 'Lab', 'title': $("#imgTitle").val(), 'caption': $("#imgDesc").val() }, }; var uploadTask = firebase.storage().ref().child('dogImages/' + selectedFile.name).put(selectedFile, metadata); uploadTask.on('state_changed', function(snapshot){ }, function(error) { } ); }
What is wrong with my selectedFile definition? Thank you for help.
source share