Angularjs 如何将多个图像发布到firebase并获取其下载url?
我正在使用离子3和firebase。我试图上传多个图像一次使用为每个循环。上传它们后,我需要获得下载url以将它们存储在数据库中。由于获取“下载url”部分是异步的,因此在上载图像的.then()部分后,我必须将数据插入数据库中……我该怎么做?这是我在提交表格后得到的结果:Angularjs 如何将多个图像发布到firebase并获取其下载url?,angularjs,firebase,firebase-realtime-database,firebase-storage,ionic3,Angularjs,Firebase,Firebase Realtime Database,Firebase Storage,Ionic3,我正在使用离子3和firebase。我试图上传多个图像一次使用为每个循环。上传它们后,我需要获得下载url以将它们存储在数据库中。由于获取“下载url”部分是异步的,因此在上载图像的.then()部分后,我必须将数据插入数据库中……我该怎么做?这是我在提交表格后得到的结果: post_news(form: NgForm){ this.title = form.value.title; this.content = form.value.content; this.cat
post_news(form: NgForm){
this.title = form.value.title;
this.content = form.value.content;
this.category = form.value.category;
console.log(this.capturedimage1);
if(this.capturedimage1 !== ''){
this.images_to_upload.push(this.capturedimage1);
}
if(this.capturedimage2 !== ''){
this.images_to_upload.push(this.capturedimage2);
}
if(this.capturedimage3 !== ''){
this.images_to_upload.push(this.capturedimage3);
}
if(this.capturedimage4 !== ''){
this.images_to_upload.push(this.capturedimage4);
}
images_url_from_db = [];
this.images_to_upload.forEach(function(item){
let storageRef = firebase.storage().ref();
const filename = Math.floor(Date.now() / 1000);
const imageRef = storageRef.child(`images/${filename}.jpg`);
imageRef.putString(item, firebase.storage.StringFormat.DATA_URL).then(data=>{
images_url_from_db.push(downloadURL);
});
})
}
可能对其他人有用…在玩了很长时间之后,我想出了一种方法…首先,我将非图像数据插入数据库(如标题、内容、类别)。然后我得到插入数据的密钥。然后我使用该键逐个上传图像,这将插入图像url,如image1、image2、image3等等
post_news(form: NgForm){
this.title = form.value.title;
this.content = form.value.content;
this.category = form.value.category;
this.news = this.afDb.list('/news');
this.news.push({title: this.title,content: this.content, category: this.category}).then(data=>{
var item_key = data.key;
console.log(data.key);
if(this.capturedimage1 !== ''){
let storageRef = firebase.storage().ref();
const filename = Math.floor(Date.now() / 1000);
const imageRef = storageRef.child(`images/${filename}.jpg`);
imageRef.putString(this.capturedimage1, firebase.storage.StringFormat.DATA_URL).then(data=>{
this.news = this.afDb.list('/news');
this.news.update(item_key, {image1: data.downloadURL});
});
}
if(this.capturedimage2 !== ''){
let storageRef = firebase.storage().ref();
const filename = 'img'+Math.floor(Date.now() / 1000);
const imageRef = storageRef.child(`images/${filename}.jpg`);
imageRef.putString(this.capturedimage2, firebase.storage.StringFormat.DATA_URL).then(data=>{
this.news = this.afDb.list('/news');
this.news.update(item_key, {image2: data.downloadURL});
});
}
}).then(data=>{
form.reset();
this.capturedimage1 = '';
this.capturedimage2 = '';
this.navCtrl.parent.select(0);
});
}