How to upload multiple files to Firebase?

前端 未结 8 1872
既然无缘
既然无缘 2020-12-04 16:44

Is there a way to upload multiple files to Firebase storage. It can upload single file within single attempt as follows.

fileButton.addEventListener(\'chang         


        
8条回答
  •  Happy的楠姐
    2020-12-04 17:23

            let ad_images=["file:///data/user/0/..../IMG-20181216-WA00001.jpg",
                           "file:///data/user/0/..../IMG-20181216-WA00002.jpg",
                           "file:///data/user/0/..../IMG-20181216-WA00003.jpg"];
            let firebase_images=[];
    
            const ref = firebase.firestore().collection('ads').doc(newRecord.id);
            putStorageItem = (url,index,ext) => {
                return firebase.storage().ref('YOURFOLDER/'+ index +'.'+ext ).putFile(url)
                .then((snapshot) => {
                    console.log(snapshot)
                    firebase_images[index] = snapshot.downloadURL;              
                    //OR
                    //firebase_images.push(snapshot.downloadURL);
                }).catch((error) => {
                    console.log('One failed:', error.message)
                });
            }
    
            Promise.all(
                ad_images.map( async (item,index) => {
                    let ext = item.split('/').pop().split(".").pop();
                    console.log(newRecord.id, item, index, ext);
                    await putStorageItem(newRecord.id, item, index, ext);
                })
            )
            .then((url) => {
                console.log(`All success`);
                console.log(firebase_images);
            })
              .catch((error) => {
                console.log(`Some failed: `, error.message)
            });
    

提交回复
热议问题