let selectedFile;
let file = document.getElementById('file');
file.addEventListener('change', function(event){
selectedFile = event.target.files[0];
});
function uploadFile(){
let fileName = selectedFile.name;
let storageRef = firebase.storage().ref('/dogImages/'+fileName);
let uploadTask = storageRef.put(selectedFile); // uploads file to firebase storge
var postData = {};
uploadTask.on('state_changed', function(snapshot){
},
function(error){
console.log(error.message);
},
function(){ //handle successful uploads
uploadTask.then((snapshot) => {
snapshot.ref.getDownloadURL().then((url) => {
postData['url'] = url; // get url of image
});
});
// updates['/users/'+postKey] = postData;
// firebase.database().ref().update(updates);
var postKey = firebase.database().ref('users/').push().key;
firebase.database().ref().child('users/'+postKey).set(postData);
firebase.database().ref().child('users/'+postKey).set("have a nice day");
});
我正在做的是将图像保存在Firebase存储器上,然后将其链接保存在Firebase数据库上。一个字符串值已成功添加到数据库,而我无法保存该对象。