[英]How to store image in firebase storage and save the image url in realtime database
[英]how can I store firebase storage image URL in Realtime database using JavaScript
此代码是在上传 firebase 存储中的图像存储时。
我想将此图像 URL 存储在 firebase 实时数据库中。
const ref = firebase.storage().ref()
const date = new Date();
const name = date.toLocaleTimeString() + '-' + file.name
const metadata = {
contentType:file.type
}
const task = ref.child(name).put(file,metadata)
task
.then(snapshot => snapshot.ref.getDownloadURL())
.then(url => {
const image = document.querySelector('#imageff')
image.src = url
})
您可以尝试执行以下 function:
const ref = firebase.storage().ref()
const date = new Date();
const name = date.toLocaleTimeString() + '-' + file.name
const metadata = {
contentType:file.type
}
const task = ref.child(name).put(file,metadata)
task
.then(snapshot => snapshot.ref.getDownloadURL())
.then((url) => {
const image = document.querySelector('#imageff')
image.src = url
//Add this URL to Firebase Realtime Database
firebase.database().ref(`/path/to/location/in/db`).set({
imageUrl: url
}).then(() => {
console.log("URL added in database successfully.")
}).catch(e => console.log(e));
})
确保.ref()
中的路径是您希望 URL 所在的位置。您还可以添加更多字段以及 imageUrl。 如果您需要更多帮助,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.