How to upload multiple files to Firebase?

前端 未结 8 1873
既然无缘
既然无缘 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条回答
  •  借酒劲吻你
    2020-12-04 17:44

    Firebase Storage uses Promise, so you can use Promises to achieve it.

    Here's the firebase blog article that covers this subject: Keeping our Promises (and Callbacks)


    Give Promise.all() an "Array of Promises"

    Promise.all(
      // Array of "Promises"
      myItems.map(item => putStorageItem(item))
    )
    .then((url) => {
      console.log(`All success`)
    })
    .catch((error) => {
      console.log(`Some failed: `, error.message)
    });
    

    Upload each file and return a Promise

    putStorageItem(item) {
      // the return value will be a Promise
      return firebase.storage().ref("YourPath").put("YourFile")
      .then((snapshot) => {
        console.log('One success:', item)
      }).catch((error) => {
        console.log('One failed:', item, error.message)
      });
    }
    

    YourPath and YourFile can be carried with myItems array (thus the item object).

    I omitted them here just for readability, but you get the concept.

提交回复
热议问题