javascript - 如何将相册上传到 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);
});
解决方案
您似乎需要同时运行多个异步函数。试试这个流程
- 创建同时上传照片的多个异步函数(承诺)。
- 收听这些上传,并每 0.1 秒/每次更改时汇总 bytesTransferred 和 totalBytes。
- 将所述 bytesTransferred 除以 totalBytes 和瞧,您的上传进度以分数表示。
感谢你的提问。这是一个有趣的。
推荐阅读
- shell - 如何从fish shell的命令历史记录中删除条目?
- python - 如何检测接触边界的轮廓?
- php - 删除大型磁盘资源上的旧文件
- ubuntu - 如何在 Ubuntu 20 上安装 bitcoind?
- python - 按下按钮后如何编辑标签
- java - 使用 IntelliJ 在 Windows 10 上使用 gradle 依赖项导入失败
- javascript - Vue 中的渲染函数和递归组件
- qt - qt creator 使用“get”创建 getter 和 setter
- java - 如果调用 matrix2.getElement(int y, int x) 我在 getElement 方法中键入什么
- laravel - laravel - 我尝试使用 href 图标从数据库中删除表中的行