首页 > 解决方案 > 如何将相册上传到 Firebase 存储?

问题描述

介绍

您好,直到今天我将一张图片上传到具有进度条的存储中,效果很好。

现在,我需要上传多张图片的相册。我认为这将是类似但迭代的东西......我正在做的是:

1. Create a blob for each image in the albun
2. Upload them one by one to the storage.

问题

问题是,如果我一张一张上传,会创建多个uploadTasks,而我只需要一个进度条。有没有办法一次上传多张图片?

代码

const { images, description, location, tags } = postInfo; // images is an array of images' uris

// TODO - Build the blobs (for each image in images)

// TODO - Post UUID = Firt Blob UUID

// Create a storage referece
const storageRef = firebase.getStorage().ref("photos").child(postId);

// TODO - Upload all blobs to storage
// const uploadTask = storageRef.put(blob); <----------- Not only a blob, just all the album

uploadTask.on("state_changed", (taskSnapshot) => { // How to get a unique uploadTask for all blobs??
  // Update progress bar
  const percent =
    (taskSnapshot.bytesTransferred / taskSnapshot.totalBytes) * 100;

  setUploadProgress(percent);
});

标签: javascriptreactjsfirebasereact-nativefirebase-storage

解决方案


您似乎需要同时运行多个异步函数。试试这个流程

  1. 创建同时上传照片的多个异步函数(承诺)。
  2. 收听这些上传,并每 0.1 秒/每次更改时汇总 bytesTransferred 和 totalBytes。
  3. 将所述 bytesTransferred 除以 totalBytes 和瞧,您的上传进度以分数表示。

感谢你的提问。这是一个有趣的。


推荐阅读